【问题标题】:Is there a way to pass a variable to the html form action attribute?有没有办法将变量传递给 html 表单操作属性?
【发布时间】:2019-05-09 20:03:44
【问题描述】:

我是编程新手,需要您的帮助 -

在我的 python/flask routes.py 脚本中,我有几个 app.routes。我想将路由的名称传递给 html 表单操作字段,以便将表单中的值发布到该名称的路由;

@app.route('/interfaceStats', methods=['GET', 'POST'])
def interfaceStats():
    routeName='interfaceStats'
    hostname = request.form['hostname']
    username = request.form['username']
    password = request.form['password']
    command = ['python', 'readOperData.py', hostname, username,password]
    print(command)
    subIntObject = subprocess.check_output(command, shell=False)
    intObjectInString = subIntObject.decode('ascii')
    interfaceObjInJsonify = jsonify(intObjectInString)
    interfaceObjInJson = json.loads(intObjectInString) 
    return render_template('interfaceStats.html', interfaceObjInJson=interfaceObjInJson, hostname=hostname)



<form id="submit-form" action={{routeName}}  method="POST">
    Hostname:
    <input type="text", name="hostname" required>
    Username:
    <input type="text" name="username" required>
    Password:
    <input type="password" name="password" required>
    <br>
    <input type="submit" id="submit-form" class="hidden" />
</form>

错误:

在服务器上找不到请求的 URL。如果您输入了 URL 请手动检查您的拼写,然后重试。

192.168.254.1 - - [09/May/2019 15:49:56] "GET /method=%22POST%22?hostname=192.168.253.144&username=admin&password=password HTTP/1.1" 404 -

【问题讨论】:

    标签: python html flask


    【解决方案1】:

    你需要在你的动作属性周围加上引号。

    <form id="submit-form" action="{{routeName}}"  method="POST">
    

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 2021-02-07
      • 2019-07-30
      • 2012-12-24
      • 1970-01-01
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      相关资源
      最近更新 更多