python - Flask - 使用表单获取数据并重定向
问题描述
我正在尝试按照https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms教程稍作修改(我正在尝试将它用于我的家庭作业任务) . 我只是想修改它,所以我会获取表单数据然后处理它。到目前为止,我只是想刷新数据,稍后我想重定向到另一个页面并在那里显示结果。问题是,无论我在表单中写什么,我都会被重定向到表单所在的主页。它甚至没有被清除!我究竟做错了什么?到目前为止,我有:
目录结构:
homework:
-app:
-templates:
-base.html
-index.html
-__init__.py
-forms.py
-routes.py
-config.py
-main.py
config.py
只包含带有一些常量作为属性的 Config 类,所以我没有包含它,main.py
只导入 app.
base.html:
<html>
<head>
<title>Weather consultant</title>
</head>
<body>
<div>
Weather consultant:
<a href="{{ url_for('index') }}">Home</a>
</div>
<hr>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
</body>
</html>
索引.html:
{% extends "base.html" %}
{% block content %}
<h1>Enter query</h1>
<form action="" method="post" novalidate>
<p>
{{ form.city.label }}<br>
{{ form.city()}}
</p>
<p>
{{ form.date.label }}<br>
{{ form.date() }}
</p>
<p>{{ form.use_dark_sky() }}
{{ form.use_dark_sky.label }}</p>
<p>{{ form.use_storm_glass() }}
{{ form.use_storm_glass.label }}</p>
<p>{{ form.use_weather_stack() }}
{{ form.use_weather_stack.label }}</p>
<p>{{ form.use_yahoo() }}
{{ form.use_yahoo.label }}</p>
<p>{{ form.use_open_weather() }}
{{ form.use_open_weather.label }}</p>
<p>{{ form.use_weather_bit() }}
{{ form.use_weather_bit.label }}</p>
<p>{{ form.use_visual_crossing() }}
{{ form.use_visual_crossing.label }}</p>
<p>{{ form.submit() }}</p>
</form>
{% endblock %}
__init__.py:
from flask import Flask
from config import Config
app = Flask(__name__)
app.config["SECRET_KEY"] = "not-so-secret"
config = Config()
from app import routes
表格.py:
class UserInputForm(FlaskForm):
city = StringField("City")
date = StringField("Enter desired date (format DD/MM/YYYY)")
use_dark_sky = BooleanField("Use DarkSky API")
use_storm_glass = BooleanField("Use Storm Glass API")
use_weather_stack = BooleanField("Use WeatherStack API")
use_yahoo = BooleanField("Use Yahoo Weather API")
use_open_weather = BooleanField(
"Use OpenWeather API - only current and near future data")
use_weather_bit = BooleanField(
"Use WeatherBit API - only current and future data")
use_visual_crossing = BooleanField("Use VisualCrossing API")
submit = SubmitField("Search")
路线.py:
@app.route("/", methods=["GET", "POST"])
def index():
form = UserInputForm()
if form.validate_on_submit():
session["form"] = form
flash('Data requested for city {}, date={}'.format(
form.city.data, form.date.data))
return redirect(url_for("index"))
return render_template("index.html", form=form)
解决方案
最初的问题是 CSRF 密钥,事实证明 API 最近发生了变化(并且默默地),请参阅https://flask-wtf.readthedocs.io/en/stable/csrf.html。编辑部分__init__.py
:
app = Flask(__name__)
app.config['SECRET_KEY'] = "not-so-secret"
csfr = CSRFProtect(app)
config = Config()
感谢@snakecharmerb 提供调试建议。
推荐阅读
- python - 如何将值列表转换为枚举名称列表?
- c++ - 给对象赋值时为什么要调用构造函数和析构函数
- windbg - WinDbg 找不到主函数的偏移量
- arrays - 如何使用对象数组创建自定义哈希表 - powershell / cmd
- javascript - 如何使用对象向元素添加属性
- python - 当您的机器上安装了 python3.6 和 python3.7 时,如何为 python3.6 安装 pip?
- c# - Request.Cookie 值未在 Visual Studio 下一次运行
- java - 重构非 REST 兼容的请求
- c - 复代数表达式的值保持为零
- crystal-reports - 如何在水晶报表中提示输入值,然后在输入值时覆盖 db 字段数据?尝试接收号码错误