烧瓶-通过表单获取数据并重定向

时间:2020-03-08 13:14:55

标签: 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仅导入了应用程序。

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>

index.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

forms.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")

routes.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)

1 个答案:

答案 0 :(得分:1)

最初的问题与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提供调试建议。