Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix csrf token label of render field macro #131

Merged
merged 2 commits into from
May 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions flask_bootstrap/templates/bootstrap/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@
{% set default_button_size = button_size or config.BOOTSTRAP_BTN_SIZE %}
{{ field(class='btn btn-%s btn-%s%s' % (button_map.get(field.name, default_button_style), default_button_size, extra_classes), **field_kwargs) }}
{% endcall %}
{%- elif field.type in ['CSRFTokenField', 'HiddenField'] -%}
{{ field()|safe }}
{%- elif field.type in ['FormField', 'FieldList'] -%}
{# note: FormFields are tricky to get right and complex setups requiring
these are probably beyond the scope of what this macro tries to do.
Expand Down
3 changes: 2 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
from flask import Flask, render_template_string
from flask_bootstrap import Bootstrap
from flask_wtf import FlaskForm
from wtforms import BooleanField, PasswordField, StringField, SubmitField
from wtforms import BooleanField, PasswordField, StringField, SubmitField, HiddenField
from wtforms.validators import DataRequired, Length


class HelloForm(FlaskForm):
username = StringField('Username', validators=[DataRequired(), Length(1, 20)])
password = PasswordField('Password', validators=[DataRequired(), Length(8, 150)])
remember = BooleanField('Remember me')
hidden = HiddenField()
submit = SubmitField()


Expand Down
4 changes: 4 additions & 0 deletions tests/test_render_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ def test():
form = hello_form()
return render_template_string('''
{% from 'bootstrap/form.html' import render_field %}
{{ render_field(form.csrf_token) }}
{{ render_field(form.hidden) }}
{{ render_field(form.username) }}
{{ render_field(form.password) }}
''', form=form)

response = client.get('/field')
data = response.get_data(as_text=True)
assert '<label class="form-control-label" for="csrf_token">CSRF Token</label>' not in data
assert '<label class="form-control-label" for="hidden">Hidden</label>' not in data
assert '<input class="form-control" id="username" name="username"' in data
assert '<input class="form-control" id="password" name="password"' in data