TemplateSyntaxError:期望令牌'=',得到'。'

时间:2016-02-27 18:20:52

标签: python flask jinja2

我正在尝试提交构建并使用flask提交表单。我的烧瓶登记表功能:

@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)

我的jinja2模板:

<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>

正如你所看到的,我有一个变量'email',我将其传递给表单。当我遍历字段时,我不想显示电子邮件字段,因为我已经拥有它,我只想将表单字段'email'设置为此值。该错误似乎与:

{% set field.data=email %}

完全追溯:

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 '.'

2 个答案:

答案 0 :(得分:1)

改变模板上下文不是一个好习惯。 但您可以尝试以下选项:

  1. 在模板中执行python并使用dict.update方法:

    {% do field.update(data=email) %}
    
  2. 尝试使用声明:

    {% with data = field["data"] %}
        {% set data=email %}
    {% endwith %}
    
  3. 尝试访问您的数据思想['数据']而不是'。'符号:

    {% set field["data"]=email %}
    

答案 1 :(得分:1)

不要在模板中改变数据或执行其他复杂逻辑。模板应该传递它所需的最终数据。

在视图中创建表单实例时设置值。如果没有数据传递,Flask-WTForms会自动使用request.form

form = RegisterForm(csrf_enabled=False, email=email)