python - Flask 从 html 模板接收 null
问题描述
我正在创建一个接受调查的网站。我正在接受问题及其相关选项并显示它们。他们成功显示,但我无法访问他们的 id 来更新他们的值。
我以前尝试过使用烧瓶的方式从多个单选按钮中请求选择,但它给了我一个错误的请求错误。
这是我的html页面:
{% extends "layout.html" %}
{% block title %}
Take Survey
{% endblock %}
{% block main %}
{% for survey in surveys %}
<h2 >{{survey.topic}}</h2> <br>
<form action="/take" method="post" >
<script>
function change( val1,val2) {
document.getElementById(val1).value = val2
}
</script>
{% for question in questions %}
<input type="hidden" name="{{question.id}}" id="{{question.id}}">
<h3>Q. {{question.question}}</h3> <br><br>
{% for option in options[question.id] %}
<input type="radio" onselect="change({{question.id}},{{option.id}})" > {{option.option}}<br>
{% endfor %}
{% endfor %}
<br>
<input type="hidden" name="survId" value="{{survey.id}}">
<input type="hidden" name="button" id="bqp">
<button class="btn btn-primary" id="continue" type="submit">Continue</button>
<button class="btn btn-primary" id ="cancel" type="submit">Cancel</button>
<script>
document.getElementById("continue").addEventListener("click", buttonFind);
document.getElementById("cancel").addEventListener("click", buttonFind);
function buttonFind() {
document.getElementById("bqp").value = this.id
}
</script>
<br>
</form>
{% endfor %}
{% endblock %}
这是我的路线:
@app.route("/take", methods= ["GET","POST"])
@login_required
def take():
if request.method =="POST":
survey = request.form.get("survId")
button = request.form.get("button")
if button == "cancel" :
searches=db.execute("SELECT * FROM surveys ORDER BY views DESC")
return render_template("homepage.html",searches=searches)
db.execute("UPDATE surveys SET views = views + 1 WHERE id=:surveyid",surveyid=survey)
questions = db.execute("SELECT id FROM questions WHERE surveyid=:surveyid", surveyid=survey)
for question in questions:
choice = request.form.get(str(question))
db.execute("UPDATE options SET choicevalue = choicevalue + 1 WHERE option=:option", option=choice)
searches=db.execute("SELECT * FROM surveys ORDER BY views DESC")
return render_template("homepage.html",searches=searches)
else:
return render_template("take.html")
在我看来,我应该从烧瓶中接收 optionid,但以下 SQL 查询最终运行,表明我收到 null:
UPDATE surveys SET views = views + 1 WHERE id='41'
DEBUG:cs50:SELECT id FROM questions WHERE surveyid='41'
DEBUG:cs50:UPDATE options SET choicevalue = choicevalue + 1 WHERE option=NULL
DEBUG:cs50:UPDATE options SET choicevalue = choicevalue + 1 WHERE option=NULL
解决方案
推荐阅读
- c++ - C ++删除向量中的对象并用新对象替换它们
- flutter - Flutter:如何在 TextField/TextFormField 中正确插入文本
- android - 如何在Android drawable中传递参数
- php - SQL:获取与每个相关的所有其他值必须在另一个表中的值
- regex - 通过递增数字替换字符串
- sorting - 编辑 Worldspace UI 层?
- c++ - 线程队列 C++
- arrays - Bash:有没有办法将 UUID 及其挂载点添加到数组中?
- python - 没有硒的网页上的Python单击按钮
- r - 正确使用 str_replace 和 str_replace_all