TemplateSyntaxError:期望令牌':',得到'}'

时间:2014-12-30 11:47:37

标签: python flask jinja2

这是我的源代码:

<ul class="filebase">
        {% for file in finance %}
        <li class="filelist">
            <div class="file-author"><a href="http://127.0.0.1:5000/uploads/files/{{file. filename}}">{{ file.filename }}</a></div>
            <div class="file-body">Description: {{ file.description }}</div>
            <div class="file-date">Date posted: {{ moment(file.date).fromNow() }}</div>
            <div class="delete-file"><a href="{{ url_for('.delete_file', file_name={{ file.filename }} ) }}">Delete File</a></div><br>
        </li>
        {% endfor %}

起初,我的代码工作正常,我突然遇到这样的错误:

TemplateSyntaxError:期望令牌':',得到'}'

这是我的追溯(如果需要):

Traceback (most recent call last):
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask\app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\flask_login.py", line 758, in decorated_view
    return func(*args, **kwargs)
  File "C:\Users\LouieCubero\Documents\GitHub\Flasky4\app\main\views.py", line 172, in list_of_finance
    return render_template('_filebase.html', finance = finance)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\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:\Users\LouieCubero\Documents\GitHub\flasky\venv\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:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\jinja2\environment.py", line 791, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\jinja2\environment.py", line 765, in _load_template
    template = self.loader.load(self, name, globals)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\jinja2\loaders.py", line 125, in load
    code = environment.compile(source, name, filename)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\jinja2\environment.py", line 554, in compile
    self.handle_exception(exc_info, source_hint=source)
  File "C:\Users\LouieCubero\Documents\GitHub\flasky\venv\lib\site-packages\jinja2\environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\LouieCubero\Documents\GitHub\Flasky4\app\templates\_filebase.html", line 8, in template
    <div class="delete-file"><a href="{{ url_for('.delete_file', file_name={{ file.filename }} ) }}">Delete File</a></div><br>
TemplateSyntaxError: expected token ':', got '}'

感谢您的帮助!

2 个答案:

答案 0 :(得分:9)

不要在其他{{}}块内执行更多{{}}。

这是我的url_for()电话:

<a href="{{ url_for('viewBlog', userid={{item.userid}}) }}"></a>

当它看起来像这样时我得到了同样的错误:

++

删除内部{{}}后,问题就消失了。

答案 1 :(得分:1)

<div class="delete-file"><a href="{{ url_for('.delete_file', file_name={{ file.filename }} ) }}">Delete File</a></div><br>

你不能像上面那样直接从django模板调用函数。编写自定义模板标签以满足您的目的。

参考django文档: https://docs.djangoproject.com/en/1.7/howto/custom-template-tags/ 或stackoverflow本身中的用户线程。 How to call function that takes an argument in a Django template?