qalis
qalis

Reputation: 1533

Flask - get data with form and redirect

I'm trying to follow https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iii-web-forms tutorial with slight modifications (I'm trying to use it for my homework task). I'm simply trying to modify it, so I'll get form data and then process it. So far I'm just trying to flash the data, later I want to redirect to another page and display the results there. The problem is, whatever I write in the form does nothing - I just get redirected to the main page, where the form is. It even does not get cleared! What am I doing wrong? So far I have:

Directories structure:

homework:
 -app:
  -templates:
   -base.html
   -index.html
  -__init__.py
  -forms.py
  -routes.py
 -config.py
 -main.py

config.py contains only Config class with some constants as attributes, so I didn't include it, and main.py only imports 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>

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)

Upvotes: 0

Views: 195

Answers (1)

qalis
qalis

Reputation: 1533

The initial problem was with CSRF key, it turns out that API has changed recently (and silently), see https://flask-wtf.readthedocs.io/en/stable/csrf.html. Edited part of __init__.py:

app = Flask(__name__)
app.config['SECRET_KEY'] = "not-so-secret"
csfr = CSRFProtect(app)
config = Config()

Thanks to @snakecharmerb for providing the debugging advice.

Upvotes: 1

Related Questions