【问题标题】:send html page data to python flask variable将html页面数据发送到python烧瓶变量
【发布时间】:2018-04-06 18:44:12
【问题描述】:

我知道这个问题已经被问过很多次了,但是这些答案对我没有帮助,请原谅我再次问。

我在 index.html 页面中获取数据并在下一个 html 页面 (annodisplay.html) 中呈现以供用户查看,然后再单击提交。 我希望仅在用户按下提交按钮后才将数据传递给 python 变量。但我没有在 python 变量中获取表单值。

这是我的 python 烧瓶代码。

app = Flask(__name__) 
@app.route('/send', methods=['GET', 'POST'])
def send():

    if request.method == 'POST':
        affflash = request.form['affflash']
        return render_template('annodisplay.html', affflash=affflash)
    return render_template('index.html')

@app.route('/get', methods=['GET', 'POST'])
def get():
    if request.method == 'POST':
        return render_template('submit.html')
    return render_template('annodisplay.html')  
    affflash = request.form['affflash']
    print affflash 
if __name__ == "__main__":
    app.run(debug=True)

这是我的 HTML index.html 代码

<form method="POST" action="/send">
<table class="tg">
  <tr>
    <td class="tg-031e">AFF_FLASH</td>
    <td class="tg-031e">
    <select id="AFF_FLASH" name="affflash">
        <option selected value="">Select</option>
        <option value="Yes">YES</option>
        <option value="No">NO</option>
    </select>
    </td>
  </tr>
</table>                
                <input class="btn btn-primary" type="submit" value="Submit">    
            </form>

这是我的 annodisplay.html 代码

<form action="{{ url_for('get') }}" method="POST">
        <body>
            <table class="tg">
  <tr>
    <td class="tg-bh7e">AFF Flash</td>
    <td class="tg-yw4l">{{ affflash }}</td>
  </tr>
</table>

            <input class="btn btn-primary" type="submit" value="Submit">
        </body>
    </form>

【问题讨论】:

  • 你能运行那个 Flask 应用程序吗?由于我看不到任何像 @app.route('/') @app.route('/index') def index(): 这样的根路由,我怎样才能到达那个 index.html
  • 是的,我可以。函数 send 对 index.html 有返回响应。 return render_template('index.html')"

标签: python html flask


【解决方案1】:

我们在通过 POST 提交的表单上使用 request.form.get() 函数(我猜在你的情况下 get 缺失) 通常我们使用常规括号。我不确定口译员是否会接受您的方括号。 希望对你有帮助

【讨论】:

  • 好的,所以 request.form.get() 和 request.values.get() 它都在工作,但我得到 None 作为值。 affflash = request.form.get('affflash')print affflash 输出为 127.0.0.1 - - [06/Apr/2018 19:48:13] "POST /send HTTP/1.1" 200 - None 127.0.0.1 - - [06/Apr/2018 19:48:18] "POST /get HTTP/1.1" 200 -
  • 不错。如果您使用的是 Python 3.x,打印内容必须在括号中:print (affflash),并且您可能应该始终从下拉列表中选择一些内容来打印值,您的默认 value 为空!。
猜你喜欢
  • 2017-10-15
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 2017-12-26
  • 1970-01-01
  • 2019-01-11
  • 1970-01-01
相关资源
最近更新 更多