python-Jinja2模板中字典的格式和使用

我正在尝试将字典form_label和form_field添加到此宏调用中,但效果不是很好.

从模板.实际上,只有宏行与该问题有关.
我再说一遍.实际上,只有宏行与该问题有关.

{% block content %}
<div id="edit_profile" class="well-lg">
  <fieldset>
    <form id="form_edit_profile" class="form-horizontal" action="{{ url|safe }}" method="post">
       <input type="hidden" name="_csrf_token" value="{{ csrf_token() }}">
       {{ macros.field(form.username, label=_("Username"), form_label={class:"col-xs-2"}, form_field={'placeholder':_("Enter your")+" "+_("Username"), class:"form-control focused required"}) }}
       ...

以及支持它的宏. (已更新,因为我不是第一次将类作为字符串传递.)

{% macro field(field, label='', form_label={},form_field={}) -%}
        <div class="form-group{% if field.errors %} error{% endif %}">
            {% set text = label or field.label.text %}

            {% if field.flags.required %}
                    {{ field.label(text=text + " *", class='control-label '+form_label['class'])}}
            {% else %}
                    {{ field.label(text=text + " ", class='control-label '+form_label['class']) }}
            {% endif %}
            <div class='col-xs-5'>
            {{ field(label, **form_field) }}
            {% if field.errors %}
                {% for error in field.errors %}
                   <label for="{{ field.id }}" class="error help-inline">{{ error }}</label>
                {% endfor %}
            {% endif %}
            </div>
        </div>
{%- endmacro %}

我想知道我是错误地引用了变量还是字典键应该是字符串还是我没有考虑的东西.

更新了回溯.

Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/Users/me/Documents/Aptana Studio 3 Workspace/project/bp_includes/lib/basehandler.py", line 89, in dispatch
    webapp2.RequestHandler.dispatch(self)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/Users/me/Documents/Aptana Studio 3 Workspace/project/bp_includes/handlers.py", line 112, in get
    return self.render_template('login.html', **params)
  File "/Users/me/Documents/Aptana Studio 3 Workspace/project/bp_includes/lib/basehandler.py", line 325, in render_template
    self.response.write(self.jinja2.render_template(filename, **kwargs))
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2_extras/jinja2.py", line 158, in render_template
    return self.environment.get_template(_filename).render(**context)
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/jinja2-2.6/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "bp_content/themes/default/templates/login.html", line 1, in top-level template code
    {% extends base_layout %}
TypeError: macro 'field' takes no keyword argument 'placeholder'

解决方法:

看起来您正在尝试从字典中引用一个没有封闭字符串的变量:

  form_label['class']

也许那是在做什么?也,

  form_label = {'class': ''}

包括评论:

您已将宏定义为“字段”,但其中的变量也被标记为“字段”.这可能会使解析器感到困惑.

上一篇:安装laravel-boilerplate遇到的一些问题


下一篇:edu_2_4_1