Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
hum4n01d
25,493 PointsFlask-WTF CSRF token missing with PyPugJs (Pug)
I’m currently writing an app with logins and I can’t get the Flask-wtf form to submit or validate because apparently my csrf token is missing. I’ve tried a bunch of different methods of embedding the token but to no avail. My guess is that somewhere in the compiling to jinja code from pug, my csrf codes are breaking. Here is the code for my log in route:
@accounts.route('/log_in', methods=['GET', 'POST'])
def log_in():
form = forms.LoginForm()
if form.validate_on_submit():
try:
models.User.get(models.User.username == form.username.data)
except models.DoesNotExist:
flash('Incorrect username or password', 'error')
if check_password_hash(user.password, form.password.data):
login_user(user)
flash('You are now logged in')
return redirect(url_for('index'))
return render_template('simple_form.pug', heading='Log in', form=form)
and here is my template (simple_form.pug)
extends mixins
block content
h1= heading
+render_form(form)
which is rendering a form using this mixin:
mixin render_field(field)
if field.errors
for error in field.errors
.notification.error= error
=field(placeholder=field.label.text)
mixin render_form(form, action='Submit')
form(method='POST', action='')
=form.hidden_tag()
each field in form
fieldset
+render_field(field)
button(type='submit')= action
This is the generated HTML code on the front end if that helps:
<form action="" method="post">
<input id="csrf_token" name="csrf_token" type="hidden" value=
"1477255131##5245edf329a5bc24d7f4fc57c7d717bfc90249dc">
<fieldset>
<input id="csrf_token" name="csrf_token" placeholder="Csrf Token"
type="hidden" value=
"1477255131##5245edf329a5bc24d7f4fc57c7d717bfc90249dc">
</fieldset>
<fieldset>
<input id="username" name="username" placeholder="Username" type=
"text" value="">
</fieldset>
<fieldset>
<input id="email" name="email" placeholder="Email Address" type=
"text" value="">
</fieldset>
<fieldset>
<input id="password" name="password" placeholder="Password" type=
"password" value="">
</fieldset>
<fieldset>
<input id="password_confirm" name="password_confirm" placeholder=
"Confirm Password" type="password" value="">
</fieldset><button type="submit">Submit</button>
</form>
Also, here is my WTForm code:
class SignupForm(FlaskForm):
username = StringField('Username', validators=[DataRequired()])
email = StringField('Email Address', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
password_confirm = PasswordField('Confirm Password', validators=[DataRequired(), EqualTo('password', message='Passwords must match')])
Any help would be much appreciated :)
2 Answers
hum4n01d
25,493 PointsThe problem apparently resolved itself! I’m guessing that my front end issue was messing things up but I really have no idea
Kenneth Love
Treehouse Guest TeacherHmm, I have zero experience with PyPug (or PyJade, which it's a clone of). What error message(s) are you getting?
hum4n01d
25,493 PointsThe error message isn’t a stack trace. It’s a form validation error that says CSRF Token Missing
Kenneth Love
Treehouse Guest TeacherHmm, but it's obviously there in the form? Can you share the Flask app?
hum4n01d
25,493 PointsOk, I’ll put it in workspaces
hum4n01d
25,493 PointsCouldn’t get workspaces to work :( Do you know of any other way to share the code? I guess I could email it to you… Is the correct email kenneth@teamtreehouse.com?
Kenneth Love
Treehouse Guest TeacherThat address will work, or you could post it to gist.github.com or something similar.
hum4n01d
25,493 PointsOk I sent the email
hum4n01d
25,493 Pointshum4n01d
25,493 Points/cc Kenneth Love The code I have is loosely based on your Build a Social Network with Flask course I took a little while
EDIT: Whoops that’s not an answer