Flask Form将数据发布到另一个html页面

时间:2017-03-07 19:30:51

标签: python flask

我有一个获取数据并重定向到bk.html页面的表单。我想从提交的表单中获取结果,并将页面重定向到该信息。我可以动态地将它放在同一页面上,但我不知道如何将它添加到新页面。

from flask import Flask, render_template, request, redirect
app = Flask(__name__)
# our index route will handle rendering our form
@app.route('/')
def index():
  return render_template("index.html")

@app.route('/users', methods=['POST'])
def create_user():
   print "Got Post Info"
   name = request.form['name']
   DojoLocation = request.form['location']
   FavoriteLanguage = request.form['Language']
   textarea = request.form['textarea']
   # redirects back to the '/' route
   return render_template('bk.html')

app.run(debug=True) # run our server
<!DOCTYPE html>
<html>
<head>
  <title>Dojo Survey Index</title>
  <meta charset="utf-8">
  <link rel='stylesheet' type='text/css' href="{{ url_for('static', filename='css/my_style_sheet.css')}}">
  <script type="text/javascript" src="{{ url_for('static', filename='js/my_script.js')}}"></script>
</head>
<body>
<div id = container 1 >
<form action='/users' method='post'>
Name: <input type='text' name='name'>
Dojo Location:  <select name="location">
<option value="Silicon Valley">Silicon Valley</option>
<option value="Seattle">Seattle</option>
<option value="Los Angeles">Los Angeles</option>
<option value="Dallas">Dallas</option>
<option value="Washington DC">Washington DC</option>
<option value="Dallas">Dallas</option>
<option value="Chicago">Chicago</option>
<option value="Berkely">Berkely</option>
<option value="Orange County">Orange County</option></select>
Favorite Language:  <select name="Language">
<option value="ios swift">ios swift</option>
<option value="Ruby on rails">ruby on rails</option>
<option value="Python">Python</option>
<option value="Mean">Mean</option>
<option value="C# ">C# DC</option>
</select>
<textarea name="textarea" value = "textbox" rows="4" cols="50" name="comment" ></textarea>

<input type='submit' value='submit'>
</form>
</div>  
</body>
</html>

1 个答案:

答案 0 :(得分:1)

返回render_template()函数时,可以将变量添加为:

var1 = 'this is a test string'
return render_template("bk.html", var1 = var1)

在您的bk.html文件中,包含以下内容以显示您希望其显示的变量。

{{ var1 }}

您可以在http://jinja.pocoo.org/

上阅读更多内容