如何将html输入到Flask? [英] How to html input to Flask?

查看:266
本文介绍了如何将html输入到Flask?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form action ='quiz_answers'> 
< p>问题1? < / p为H.
< input type =radioname =q1value =2> Answer1< / input>
< input type =radioname =q1value =1> Answer2< / input>
< input type =radioname =q1value =0> Answer3< / input>
< input type =radioname =q1value =0> Answer4< / input>

< p>问题2? < / p为H.
< input type =radioname =q2value =2> Answer1< / input>
< input type =radioname =q2value =1> Answer2< / input>
< input type =radioname =q2value =0> Answer3< / input>
< input type =radioname =q2value =0> Answer4< / input>
< / form>

和这个python代码:

<$ p $ ($)
def测验():
返回render_template() 'quiz.html')

@ app.route('/ quiz_answers',methods = ['POST'])
def quiz_answers():
q1 = request.form ['q1']
q2 = request.form ['q2']
q4 = request.form ['q4']
q5 = request.form ['q5']

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

如何添加一个按钮,在点击+问题1和问题2之后,打开一个新模板并显示结果?所以总之,我怎么做一个按钮,说:是的,问题已经回答,计数的价值,并返回他们在一个新的HTML页面?

The Flask 快速入门教程不会通过HTTP请求,但在这种特定情况下不会回答我的问题。谷歌搜索只取得了这个stackoverflow线程没有把我任何地方。

解决方案

您应该可以添加提交将POST或GET数据响应的形式返回到动作



在这种情况下,您可能需要修改您的表单标记定义为:

 < form action =/ quiz_answersmethod =POST> 

然后添加一个提交按钮:

 < input type =submitvalue =提交! /> 

当用户点击时,它应该生成一个POST请求回到 http: // your_server / quiz_answers


I have this html bit:

<form action='quiz_answers'>
    <p> Question1? </p>
    <input type="radio" name="q1" value="2">Answer1</input>
    <input type="radio" name="q1" value="1">Answer2</input>
    <input type="radio" name="q1" value="0">Answer3</input>
    <input type="radio" name="q1" value="0">Answer4</input>

    <p> Question2? </p>
    <input type="radio" name="q2" value="2">Answer1</input>
    <input type="radio" name="q2" value="1">Answer2</input>
    <input type="radio" name="q2" value="0">Answer3</input>
    <input type="radio" name="q2" value="0">Answer4</input>
</form>

and this python code:

from flask import Flask, render_template, request

@app.route('/quiz')
def quiz():
    return render_template('quiz.html')

@app.route('/quiz_answers', methods=['POST'])
def quiz_answers():
    q1 = request.form['q1']
    q2 = request.form['q2']
    q4 = request.form['q4']
    q5 = request.form['q5']

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

How would I go about adding making a button which, after it has been clicked on + question 1 and 2 have been answered, opens a new template with the results? So in short, how do I make a button saying "Yup, the questions have been answered, count the values and return them in a new HTML page"?

The Flask Quick Start tutorial does go trough HTTP requests but doesn't answer my quetion in this specific situation. Googling only yielded this stackoverflow thread which didn't bring me anywhere.

解决方案

You should be able to add a submit button to the form to POST or GET the data response back to the action.

In this case, you will probably want to modify your form tag definition to:

<form action="/quiz_answers" method="POST">

And add a submit button like this:

<input type="submit" value="Submit!" />

When the user clicks, it should generate a POST request back to http://your_server/quiz_answers.

这篇关于如何将html输入到Flask?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆