【发布时间】:2020-12-23 13:55:45
【问题描述】:
你知道为什么这个 HTML 表单不提交吗? 我只是没有在 Flask 中收到“POST”消息,因此无法从那里执行我想要的。
HTML 部分(带有“取消隐藏”测试的按钮)
<button id="take_test" class="btn btn-info" onclick="Showtest()">Take the test</button>
<form style="visibility:hidden" id="addiction2" action:"/test" method:"post">
<div class="form-group">
<fieldset id:"group1">
<p>Over the last 12 months, did you experience an impaired control over gaming (frequency, intensity, duration, termination, context)?</p>
<input type="radio" id="1" name="group1" value="1" required>
<label for="1">Yes</label><br>
<input type="radio" id="0" name="group1" value="0" required>
<label for="0">No</label><br>
</fieldset>
</div>
<div class="form-group">
<fieldset id:"group2">
<p>Over the last 12 months, did you experience an increasing priority given to gaming to the extent that gaming takes precendence over other life interests and daily activities?:</p>
<input type="radio" id="1" name="group2" value="1" required>
<label for="1">Yes</label><br>
<input type="radio" id="0" name="group2" value="0" required>
<label for="0">No</label><br>
</fieldset>
</div>
<div class="form-group">
<fieldset id:"group3">
<p>Over the last 12 months, did you experience a continuation or escalation of gaming despite the occurance of negative consequences (i.e. significant impairment in personal, family, social, educational, occupational or other important areas of functioning)?</p>
<input type="radio" id="1" name="group3" value="1" required>
<label for="1">Yes</label><br>
<input type="radio" id="0" name="group3" value="0" required>
<label for="0">No</label><br>
</fieldset>
</div>
<div class="form-group">
<fieldset id:"group4">
<p>Over the last 12 months, did you try to stop gaming or reduce the time spent gaming, but failed to do that?</p>
<input type="radio" id="1" name="group4" value="1" required>
<label for="1">Yes</label><br>
<input type="radio" id="0" name="group4" value="0" required>
<label for="0">No</label><br>
</fieldset>
</div>
<div class="form-group">
<fieldset id:"group5">
<p>Over the last 12 months, did you ever lie to your spouse, family or friends about the amount of time you have spent gaming?</p>
<input type="radio" id="1" name="group5" value="1" required>
<label for="1">Yes</label><br>
<input type="radio" id="0" name="group5" value="0" required>
<label for="0">No</label><br>
</fieldset>
</div>
<br>
<button class="btn btn-secondary" type="submit">Submit</button>
</form>
</main>
<script>
function Showtest() {
var x = document.getElementById("addiction2");
x.style.visibility = "visible";
var y = document.getElementById("take_test");
y.style.display = "none";
var z = document.getElementById("footer");
z.style.display = "block";
}
</script>
我尝试使用 post 方法将变量添加到分数中的 Python 部分(我知道这可能可以以更智能的方式完成,但现在甚至没有收到 POST(如日志所示)并且重定向不会发生)
@app.route("/test", methods=["GET", "POST"])
@login_required
def test():
if request.method == "POST":
#get all the answers, store them in variables as 1 or 0
q1 = request.form['group1']
q2 = request.form['group2']
q3 = request.form['group3']
q4 = request.form['group4']
q5 = request.form['group5']
score = q1+q2+q3+q4+q5
print(score)
#mark database as test completed for this user
return redirect("/results")
【问题讨论】:
-
action和method属性的form元素使用冒号而不是等号。会不会是这个问题?