Jinja2 отладка - PullRequest
       11

Jinja2 отладка

0 голосов
/ 20 мая 2019

У меня есть довольно много шаблонов всего в jinja2 в моем приложении фляги. Проблема, которую я имею, Werkzeug Debugger не может указать мне точное местоположение ошибки, он только высвечивает синтаксическую ошибку шаблона jinja2.exceptions.TemplateSyntaxError: tag name expected. Есть ли способ, которым я могу перенастроить его, чтобы указать точное местоположение ошибки? ... иначе это боль в кости для отладки.

Я просмотрел свои сценарии, но все они, кажется, в порядке. Я даже пытался комментировать некоторые безуспешно. И да, я добавил следующее в свой конфигурационный файл TEMPLATE_DEBUG = False после проведения раундов исследования.

Мой след стека:

`jinja2.exceptions.TemplateSyntaxError

jinja2.exceptions.TemplateSyntaxError: ожидается имя тега Traceback (последний вызов был последним)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/app.py", line 2309, in __call__

return self.wsgi_app(environ, start_response)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/app.py", line 2295, in wsgi_app

response = self.handle_exception(e)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router

return original_handler(e)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/app.py", line 1741, in handle_exception

reraise(exc_type, exc_value, tb)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise

raise value

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app

response = self.full_dispatch_request()

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 269, in error_router

return original_handler(e)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise

raise value

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request

rv = self.dispatch_request()

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/app/main/index/views.py", line 24, in index

search_form=search_form)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/templating.py", line 135, in render_template

context, ctx.app)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/flask/templating.py", line 117, in _render

rv = template.render(context)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render

return original_render(self, *args, **kwargs)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render

return self.environment.handle_exception(exc_info, True)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception

reraise(exc_type, exc_value, tb)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise

raise value.with_traceback(tb)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/app/templates/base.html", line 48, in template

<li {% - if request.path == url_for('user.index') %} class="active"{%- endif -%} ><a

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/environment.py", line 1005, in render

return concat(self.root_render_func(self.new_context(vars)))

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/app/main/index/templates/index.html", line 1, in top-level template code

{% extends "base.html" %}

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception

reraise(exc_type, exc_value, tb)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise

raise value.with_traceback(tb)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/app/templates/base.html", line 48, in template

<li {% - if request.path == url_for('user.index') %} class="active"{%- endif -%} ><a

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/environment.py", line 497, in _parse

return Parser(self, source, name, encode_filename(filename)).parse()

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 901, in parse

result = nodes.Template(self.subparse(), lineno=1)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 883, in subparse

rv = self.parse_statement()

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 130, in parse_statement

return getattr(self, 'parse_' + self.stream.current.value)()

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 268, in parse_block

node.body = self.parse_statements(('name:endblock',), drop_needle=True)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 165, in parse_statements

result = self.subparse(end_tokens)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 883, in subparse

rv = self.parse_statement()

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 130, in parse_statement

return getattr(self, 'parse_' + self.stream.current.value)()

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 223, in parse_if

drop_needle=True)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 165, in parse_statements

result = self.subparse(end_tokens)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 883, in subparse

rv = self.parse_statement()

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 125, in parse_statement

self.fail('tag name expected', token.lineno)

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/venv/lib/python3.7/site-packages/jinja2/parser.py", line 59, in fail

raise exc(msg, lineno, self.name, self.filename)

jinja2.exceptions.TemplateSyntaxError: tag name expected`

Если я смогу получить помощь, с точки зрения точного указания мне, откуда возникла ошибка шаблона jinja2, я буду очень благодарен ... принесите хаки, спасибо заранее.

1 Ответ

1 голос
/ 20 мая 2019

Вы можете видеть строку, вызывающую проблемы в трассировке стека:

File "/doka/Desktop/Python_projects/flask_collection/flaskbook/app/templates/base.html", line 48, in template
<li {% - if request.path == url_for('user.index') %} class="active"{%- endif -%} ><a

Как говорят в документации Jinja:

Вы не должны добавлять пробел между тегом и знаком минус.

действительный:

{%- if foo -%}...{% endif %}

недействительный:

{% - if foo - %}...{% endif %}

Больше: http://jinja.pocoo.org/docs/2.10/templates/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...