url jinja2 模板中的函数在 Django 中抛出 AttributeError
url function in jinja2 template throws AttributeError in Django
我在 my_project/jinja2/
中有一个 base.html
,它包含站点的 <body>
以外的所有内容。然后我们像一个一样扩展 base.html
。
在这个例子中,我在 my_project/people/jinja2/people/people_list.html
的文件中从应用程序 people
扩展 base.html
,使用如下:
{% extends "base.html" %}
{% block content %}
<!-- Some html -->
{% endblock content %}
我的 base.html
在 <head>
中包含一个 link 到博客 rss 提要,如下所示:
<link rel="alternate" type="application/atom+xml" title="Blog" href="{{ url('blog:articles_feed') }}" />
这在大多数地方都能正常工作,例如 my_project/jinja2/index.html
和 my_project/jinja2/blog_list.html
,但在第 3 个应用程序 people
中,我在同一行模板代码中收到以下错误:
AttributeError: 'str' object has no attribute '__call__'
因为 jinja2 有更好的调试功能,我可以 运行 python 在 werkzueg 中查看可能发生的事情的一些潜在细节:
locals()
输出:
{
'_': {...},
'static': < bound method StaticFilesStorage.url of < django.contrib.staticfiles.storage.StaticFilesStorage object at 0x7f423ec3ebe0 >> ,
'joiner': < class 'jinja2.utils.Joiner' > ,
'request': < WSGIRequest: GET '/case-studies/hog?__debugger__=yes&cmd=locals()&frm=139922493301984&s=XfAagGnpxRWFBRRd0Uzk' > ,
'page': None,
'csrf_input': < django.utils.functional.lazy. < locals > .__proxy__ object at 0x7f423e8262e8 > ,
'cycler': < class 'jinja2.utils.Cycler' > ,
'dict': < class 'dict' > ,
'absolute_url': < function absolute_url at 0x7f423ece5b70 > ,
'lipsum': < function generate_lorem_ipsum at 0x7f423ebfc8c8 > ,
'view': < leaf.views.LeafTemplateView object at 0x7f423e820198 > ,
'range': < class 'range' > ,
'ngettext': < function ungettext at 0x7f42450547b8 > ,
'gettext': < function ugettext at 0x7f4245054730 > ,
'absolute_root': < function absolute_root at 0x7f423ece8268 > ,
'datetime': < class 'datetime.datetime' > ,
'csrf_token': < django.utils.functional.lazy. < locals > .__proxy__ object at 0x7f423e826400 > ,
'url': 'people/all'
}
我不确定这是否是一个正确的假设,但是 url
是否没有作为函数正确添加到环境中,而是作为字符串添加到环境中?它在我的 jinja2.py 文件中,并且在 jinja2 模板中按预期在其他地方工作。给出了什么?
。我使用的是 jinja 模板,kwargs
是自动添加的。我的 kwargs
之一是 <url>
并且覆盖了我添加到 jinja 环境中的 url
扩展。将 kwarg
重命名为其他名称解决了问题。
我在 my_project/jinja2/
中有一个 base.html
,它包含站点的 <body>
以外的所有内容。然后我们像一个一样扩展 base.html
。
在这个例子中,我在 my_project/people/jinja2/people/people_list.html
的文件中从应用程序 people
扩展 base.html
,使用如下:
{% extends "base.html" %}
{% block content %}
<!-- Some html -->
{% endblock content %}
我的 base.html
在 <head>
中包含一个 link 到博客 rss 提要,如下所示:
<link rel="alternate" type="application/atom+xml" title="Blog" href="{{ url('blog:articles_feed') }}" />
这在大多数地方都能正常工作,例如 my_project/jinja2/index.html
和 my_project/jinja2/blog_list.html
,但在第 3 个应用程序 people
中,我在同一行模板代码中收到以下错误:
AttributeError: 'str' object has no attribute '__call__'
因为 jinja2 有更好的调试功能,我可以 运行 python 在 werkzueg 中查看可能发生的事情的一些潜在细节:
locals()
输出:
{
'_': {...},
'static': < bound method StaticFilesStorage.url of < django.contrib.staticfiles.storage.StaticFilesStorage object at 0x7f423ec3ebe0 >> ,
'joiner': < class 'jinja2.utils.Joiner' > ,
'request': < WSGIRequest: GET '/case-studies/hog?__debugger__=yes&cmd=locals()&frm=139922493301984&s=XfAagGnpxRWFBRRd0Uzk' > ,
'page': None,
'csrf_input': < django.utils.functional.lazy. < locals > .__proxy__ object at 0x7f423e8262e8 > ,
'cycler': < class 'jinja2.utils.Cycler' > ,
'dict': < class 'dict' > ,
'absolute_url': < function absolute_url at 0x7f423ece5b70 > ,
'lipsum': < function generate_lorem_ipsum at 0x7f423ebfc8c8 > ,
'view': < leaf.views.LeafTemplateView object at 0x7f423e820198 > ,
'range': < class 'range' > ,
'ngettext': < function ungettext at 0x7f42450547b8 > ,
'gettext': < function ugettext at 0x7f4245054730 > ,
'absolute_root': < function absolute_root at 0x7f423ece8268 > ,
'datetime': < class 'datetime.datetime' > ,
'csrf_token': < django.utils.functional.lazy. < locals > .__proxy__ object at 0x7f423e826400 > ,
'url': 'people/all'
}
我不确定这是否是一个正确的假设,但是 url
是否没有作为函数正确添加到环境中,而是作为字符串添加到环境中?它在我的 jinja2.py 文件中,并且在 jinja2 模板中按预期在其他地方工作。给出了什么?
kwargs
是自动添加的。我的 kwargs
之一是 <url>
并且覆盖了我添加到 jinja 环境中的 url
扩展。将 kwarg
重命名为其他名称解决了问题。