python - Flask_form : CSRF Token do not match -
i'm using flask_form in flask application , have being stucked hours 'csrf token not match'.
<form method="post" action="{{ url_for('auth.login') }}" role="form"> {{ form.hidden_tag() }} {{ wtf.form_errors(form, hiddens="only") }} {{ wtf.form_field(form.email)}} {{ wtf.form_field(form.password)}} <p><button type="submit">login</button></p> </form>
views.py
@auth.route('/login', methods=['get', 'post']) def login(): form = loginform() if form.validate_on_submit(): print('login form received on server , valid') # check whether user exists in database , whether # password entered matches password in database user = user.query.filter_by(email=form.email.data).first() if user not none , user.verify_password(form.password.data) , check_password_hash(user.pwd, form.password.data): # log employee in login_user(user) #,remember=true) # redirect home page after login return redirect(url_for('grapher.upload')) # when login details incorrect else: flash('invalid email or password.', 'info') # load login template return render_template('auth/login.html', form=form, title='login')
form
class loginform(flaskform): email = stringfield('email', validators=[datarequired(), email(), length(min=1,max=254, message='the maximum length of filed 254 characters')]) password = passwordfield('password', validators=[datarequired(), length(max=20, message='password maximium length 20 characters.')])
why error?
you need add csrf input field in form said in docs:
<form method="post"> {{ form.csrf_token }} </form>
every wtforms validation checks availability of token in post request data unless explicitly disabled.
Comments
Post a Comment