Reputation: 36205
I am trying to submit a build and submit a form using flask. My flask registration form function:
@blueprint.route("/register/", defaults={'token': ''}, methods=['GET', 'POST'])
@blueprint.route("/register/<token>", methods=['GET', 'POST'])
def register(token):
form = RegisterForm(request.form, csrf_enabled=False)
email = confirm_token(token)
if form.validate_on_submit():
new_user = User.create(username=form.username.data,
first_name=form.first_name.data,
last_name=form.last_name.data,
email=form.email.data,
password=form.password.data,
active=True)
new_user = User.create(email=form.email.data, password=form.password.data, active=True)
flash("Thank you for registering. You can now log in.", 'success')
return redirect(url_for('public.home'))
else:
flash_errors(form)
return render_template('public/register.html', form=form , email=email)
My jinja2 template:
<h1>Register</h1>
<br/>
<form id="registerForm" class="form form-register" method="POST" action="" role="form">
{% for field in form %}
{% print(field.name)%}
{% if field.label.text =='Email' %}
{% set field.data=email %}
{% else %}
{{ render_field(field) }}
{% endif %}
{% endfor %}
<p><input class="btn btn-default btn-submit" type="submit" value="register"></p>
</form>
As you can see I have a variable 'email' which I am passing into the form. As I loop through the fields I don't want to display the email field since I already have it, I just want to set the form field 'email' to this value. The error appears to be with:
{% set field.data=email %}
full traceback:
Traceback (most recent call last):
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask_debugtoolbar\__init__.py", line 124, in dispatch_request
return view_func(**req.view_args)
File "C:\envs\r2\mini\myflaskapp\views\public.py", line 73, in register
return render_template('public/register.html', form=form , email=email)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\flask\templating.py", line 127, in render_template
return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\jinja2\environment.py", line 830, in get_or_select_template
return self.get_template(template_name_or_list, parent, globals)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\jinja2\environment.py", line 791, in get_template
return self._load_template(name, self.make_globals(globals))
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\jinja2\environment.py", line 765, in _load_template
template = self.loader.load(self, name, globals)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\jinja2\loaders.py", line 125, in load
code = environment.compile(source, name, filename)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\jinja2\environment.py", line 554, in compile
self.handle_exception(exc_info, source_hint=source)
File "C:\envs\virtalenvs\flask_mini\lib\site-packages\jinja2\environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\envs\r2\mini\myflaskapp\templates\public\register.html", line 15, in template
{% set field.data=email %}
TemplateSyntaxError: expected token '=', got '.'
Upvotes: 0
Views: 1817
Reputation: 127180
Don't mutate data or perform other complex logic in the template. The template should be passed the final data it needs.
Set the value when creating the form instance in the view. Flask-WTForms automatically uses request.form
if no data is passed.
form = RegisterForm(csrf_enabled=False, email=email)
Upvotes: 1
Reputation: 22021
It's not a good practice to mutate template context. however you can try options below:
Execute python in template and use dict.update method:
{% do field.update(data=email) %}
Try to use with statement:
{% with data = field["data"] %}
{% set data=email %}
{% endwith %}
Try to access your data thought ['data'] instead of '.' notation:
{% set field["data"]=email %}
Upvotes: 1