每个web应用都需要使用表单来采集用户数据。现在让我们使用Flask框架创建一个简单的表单来收集用户的基本信息,例如名称、年龄、邮件、兴趣爱好等,我们将这个模板文件命名为bio_form.html。
Bio Data Form
视图函数bio_data_form同时支持POST和GET请求。GET请求将渲染bio_form.html模板,而POST请求将重定向到showbio:
@app.route('/form', methods=['POST', 'GET'])def bio_data_form(): if request.method == "POST": username = request.form['username'] age = request.form['age'] email = request.form['email'] hobbies = request.form['hobbies'] return redirect(url_for('showbio', username=username, age=age, email=email, hobbies=hobbies)) return render_template("bio_form.html")
相关推荐:《Python视频教程》
下面是showbio的实现:
@app.route('/showbio', methods=['GET']) def showbio(): username = request.args.get('username') age = request.args.get('age') email = request.args.get('email') hobbies = request.args.get('hobbies') return render_template("show_bio.html", username=username, age=age, email=email, hobbies=hobbies)
以及show_bio.html的内容:
Bio-Data Details Bio-Data Details Username: {{ username }} Email: {{ email }} Hobbies: {{ hobbies }}
相关推荐:
Flask框架如何使用HTML模板
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)