Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python jinja2模板中的url函数在Django中抛出AttributeError_Python_Html_Django_Jinja2 - Fatal编程技术网

Python jinja2模板中的url函数在Django中抛出AttributeError

Python jinja2模板中的url函数在Django中抛出AttributeError,python,html,django,jinja2,Python,Html,Django,Jinja2,我在myu project/jinja2/中有一个base.html,它包含除站点的之外的所有内容。然后我们扩展base.html 在本例中,我在my_project/people/jinja2/people/people_list.html文件中从apppeople扩展base.html,如下所示: {% extends "base.html" %} {% block content %} <!-- Some html --> {% endblock content %} 这在

我在
myu project/jinja2/
中有一个
base.html
,它包含除站点的
之外的所有内容。然后我们扩展
base.html

在本例中,我在
my_project/people/jinja2/people/people_list.html
文件中从app
people
扩展
base.html
,如下所示:

{% extends "base.html" %}

{% block content %}
<!-- Some html -->
{% endblock content %}
这在大多数地方都能正常工作,例如
my_project/jinja2/index.html
my_project/jinja2/blog_list.html
,但在这第三个应用程序
people
,我在同一行模板代码中遇到以下错误:

AttributeError: 'str' object has no attribute '__call__'
由于jinja2具有更好的调试功能,我可以在werkzueg中运行python,并了解可能发生的情况的一些潜在细节:

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'
}
{
'_': {...},
“static”:>,
“joiner”:,
“请求”:,
“页面”:无,
“csrf\u输入”:0x7f423e8262e8>处的。\uuuuu代理\uuuuu对象,
'循环器':,
“dict”:,
“绝对url”:<0x7f423ece5b70处的函数绝对url>,
“lipsum”:,
“视图”:,
'范围':,
“ngettext”:<0x7f42450547b8处的函数ungettext>,
'gettext':,
“绝对根”:《0x7f423ece8268处的函数绝对根》,
'datetime':,
“csrf\u令牌”:。\uuuuuuu proxy\uuuuuuuuuu对象位于0x7f423e826400>,
“url”:“人/所有人”
}

我不确定这是否是一个正确的假设,但是
url
是否不是作为一个函数正确地添加到环境中,而是作为一个字符串?它在我的jinja2.py文件中,并在jinja2模板中预期的其他地方工作。给你什么?

。我使用的是jinja模板,
kwargs
是自动添加的。我的一个
kwargs
,它超越了我添加到jinja环境中的
url
扩展。将
kwarg
重命名为其他名称解决了此问题。

您可能正在
人员
视图中传递一个上下文变量,也称为
url
。有趣的一点。我从来没有考虑过这样的事情。我在
people
应用程序中设置了如下url路径:
url(r'^people list/',TemplateView.as_view(template_name=“people_list.html”),
。从中,我没有看到
TemplateView
添加
url
上下文变量。使用jinja,会不会有其他东西意外地为这个视图添加上下文(在其他一些视图中效果很好)。
{
    '_': {...},
    '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'
}