From d08f8a747e25f1a5063218eab347ce79de848c5a Mon Sep 17 00:00:00 2001 From: yuxiaoy Date: Sun, 16 May 2021 23:00:20 +0800 Subject: [PATCH 1/2] Fix csrf token label of render field macro --- flask_bootstrap/templates/bootstrap/form.html | 2 ++ tests/test_render_field.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/flask_bootstrap/templates/bootstrap/form.html b/flask_bootstrap/templates/bootstrap/form.html index 43c9ff82..b64f74ee 100644 --- a/flask_bootstrap/templates/bootstrap/form.html +++ b/flask_bootstrap/templates/bootstrap/form.html @@ -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 == 'CSRFTokenField' -%} + {{ 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. diff --git a/tests/test_render_field.py b/tests/test_render_field.py index 6abc5fa2..c59962a5 100644 --- a/tests/test_render_field.py +++ b/tests/test_render_field.py @@ -7,11 +7,13 @@ def test(): form = hello_form() return render_template_string(''' {% from 'bootstrap/form.html' import render_field %} + {{ render_field(form.csrf_token) }} {{ render_field(form.username) }} {{ render_field(form.password) }} ''', form=form) response = client.get('/field') data = response.get_data(as_text=True) + assert '' not in data assert ' Date: Mon, 17 May 2021 22:17:01 +0800 Subject: [PATCH 2/2] Add support for HiddenField --- flask_bootstrap/templates/bootstrap/form.html | 2 +- tests/conftest.py | 3 ++- tests/test_render_field.py | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/flask_bootstrap/templates/bootstrap/form.html b/flask_bootstrap/templates/bootstrap/form.html index b64f74ee..4a74c76f 100644 --- a/flask_bootstrap/templates/bootstrap/form.html +++ b/flask_bootstrap/templates/bootstrap/form.html @@ -121,7 +121,7 @@ {% 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 == 'CSRFTokenField' -%} + {%- 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 diff --git a/tests/conftest.py b/tests/conftest.py index b09e315b..71a60f69 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,7 +2,7 @@ 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 @@ -10,6 +10,7 @@ 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() diff --git a/tests/test_render_field.py b/tests/test_render_field.py index c59962a5..52b380dc 100644 --- a/tests/test_render_field.py +++ b/tests/test_render_field.py @@ -8,6 +8,7 @@ def test(): 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) @@ -15,5 +16,6 @@ def test(): response = client.get('/field') data = response.get_data(as_text=True) assert '' not in data + assert '' not in data assert '