python-3.x 我能以某种方式将HTML中的输入标记(文本)的数据作为变量(没有表单)获取到Flask应用程序中吗?

wyyhbhjk  于 2023-03-31  发布在  Python
关注(0)|答案(1)|浏览(96)

或者有没有什么解决方案可以用url_for href和a html标签传递多个变量?
我的问题是,我不能用form获取input标签的数据,因为我使用了一个动态URL,它看起来像:

<a href="{{ url_for('polling', Qid=question.name, option=1) }}"> Vote </a>

我想在路由中使用input标签的数据:

@app.route('/polling/<Qid>/<option>')
def polling(Qid, option):
    return redirect(url_for('results', Qid=Qid))
8oomwypt

8oomwypt1#

回答你的部分问题:
或者有没有什么解决方案可以用一个html标签和一个url_for href来传递多个变量?
是的,你可以将多个变量传递给url_for,没有任何问题:
main.py

from flask import Flask, url_for, redirect, render_template

app = Flask(__name__)

@app.route("/")
def index():
    return render_template('home.html')

@app.route("/polling/<Qid>/<option>")
def polling(Qid, option):
    return redirect(url_for('results', Qid=Qid))

@app.route("/results/<Qid>")
def results(Qid):
    return "Redirected Correctly to Question: " + Qid 

if __name__ == '__main__':
    app.run(debug=True)

home.html

<!doctype html>

{% set questionname = 'Q1' %}

<a href="{{ url_for('polling', Qid=questionname, option=1)}}">Vote</a>

相关问题