简体   繁体   中英

Jinja template has no hidden tags and validate_on_submit not working

I am trying to make a weather application.. I am using flask. I am getting the following error:

jinja2.exceptions.UndefinedError: 'forms.WeatherForm object' has no attribute 'hidden_tag'

My code below:
Weather.html

{% extends 'layout.html' %}

{% block content %}
<form method="POST" action="" autocomplete="off">
        <!-- {{ form.hidden_tag() }} -->
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">Place</legend>

            <div class="form-group">
                {{ form.place.label(class="form-control-label") }}
                {% if form.place.errors %}
                    {{ form.place(class="form-control form-control-lg is-invalid") }}
                    <div class="invalid-feedback">
                        {% for error in form.place.errors %}
                            <span>{{ error }}</span>
                        {% endfor %}
                    </div>
                {% else %}
                    {{ form.place(class="form-control form-control-lg") }}
                {% endif %}
            </div>
        </fieldset>
        <div class="form-group">
            {{ form.submit(class="btn btn-outline-info") }}
        </div>

    </form>


{% endblock content %}

route.py:

from flask import (render_template, url_for, flash,
               redirect, request, Flask)
from forms import WeatherForm
app = Flask(__name__)
@app.route("/")
@app.route("/home")
def home():
    return render_template('home.html')

@app.route('/weather', methods=['GET', 'POST'])
def weather():
    form = WeatherForm()
    # if form.validate_on_submit():
    # return redirect('url_for("result")')
    return render_template('weather.html', form=form)


@app.route('/result',methods=['POST'])
def result():
    render_template('result.html')

if __name__ == '__main__':
    app.run(debug=True)

I have commented the if statement bcz I am getting another error...

AttributeError: 'WeatherForm' object has no attribute 'validate_on_submit'

forms.py:

from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
from wtforms.validators import DataRequired


class WeatherForm():
    place = StringField('Place', validators=[DataRequired()])
    submit = SubmitField('Post')

Not sure if I have not imported some functionalities for the above errors.. I am new to flask... Thanks for the help

Edit: Found another solution..If the csrf does not work, we can set a SECRT_KEY... Another answer that helps after my first error

Consider implementing FlaskForm into your WeatherForm as such:

class WeatherForm(FlaskForm):
    place = StringField('Place', validators=[DataRequired()])
    submit = SubmitField('Post')

Also, implement CSRF protection on the app:

from flask_wtf.csrf import CSRFProtect

# Add this somewhere in your app's initialization
csrf = CSRFProtect(app)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM