Django 变量:在 url 中声明
在 urls.py 中:
(r'^bbb/id(?P<user_id>[0-9]+)/$', 'django.views.generic.simple.direct_to_template,
{'template': 'some.html', 'extra_context': {'user_id': user_id}}),
在 some.html 中: {{ user_id }}
但有一个错误: name 'user_id' is not Defined (in urls.py)
所以,如何在 urls.py 中声明该变量并将其直接发送到“some.html”???
谢谢。
In urls.py:
(r'^bbb/id(?P<user_id>[0-9]+)/
In some.html: {{ user_id }}
But there is an error: name 'user_id' is not defined (in urls.py)
So, how to declare that variable in urls.py and send it directly to 'some.html'???
Thanks.
, 'django.views.generic.simple.direct_to_template,
{'template': 'some.html', 'extra_context': {'user_id': user_id}}),
In some.html: {{ user_id }}
But there is an error: name 'user_id' is not defined (in urls.py)
So, how to declare that variable in urls.py and send it directly to 'some.html'???
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要将其放入
extra_context
中。它已在 URL 中捕获,因此存在于模板的params
字典中:{{ params.user_id }}
。请参阅文档< /a> - 还要注意,这些旧的基于函数的通用视图已被弃用,您应该使用基于类的 TemplateView。
You don't need to put it in
extra_context
. It's already captured in the URL, so is present in theparams
dictionary in the template:{{ params.user_id }}
.See the documentation - and also note that these old function-based generic views are deprecated, and you should be using the class-based TemplateView.
该变量确实没有在该 python 代码中声明。 :) 您不需要在视图上下文中设置此变量。该视图将接收名为 **kwargs 的命名匹配。
The variable really isn't declared in that python code. :) You do not need to set this variable in the view context. The view will receive named matches as **kwargs.