在 django 中创建我自己的上下文处理器
我已经到了需要将某些变量传递给我的所有视图(主要是自定义身份验证类型变量)的地步。
有人告诉我编写自己的上下文处理器是执行此操作的最佳方法,但我遇到了一些问题。
我的设置文件如下所示
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages",
"sandbox.context_processors.say_hello",
)
如您所见,我有一个名为“context_processors”的模块和一个名为“say_hello”的函数。
看起来
def say_hello(request):
return {
'say_hello':"Hello",
}
我是否正确地假设我现在可以在我的观点范围内执行以下操作?
{{ say_hello }}
现在,这在我的模板中没有呈现任何内容。
我的观点看起来像
from django.shortcuts import render_to_response
def test(request):
return render_to_response("test.html")
I have come to a point where I need to pass certain variables to all of my views (mostly custom authentication type variables).
I was told writing my own context processor was the best way to do this, but I am having some issues.
My settings file looks like this
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.contrib.messages.context_processors.messages",
"sandbox.context_processors.say_hello",
)
As you can see, I have a module called 'context_processors' and a function within that called 'say_hello'.
Which looks like
def say_hello(request):
return {
'say_hello':"Hello",
}
Am I right to assume I can now do the following within my views?
{{ say_hello }}
Right now, this renders to nothing in my template.
My view looks like
from django.shortcuts import render_to_response
def test(request):
return render_to_response("test.html")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您编写的上下文处理器应该可以工作。问题出在你的看法上。
您确定您的视图是通过
RequestContext
渲染的吗?例如:
上面的视图不会使用
TEMPLATE_CONTEXT_PROCESSORS
中列出的上下文处理器。确保您提供一个RequestContext
,如下所示:The context processor you have written should work. The problem is in your view.
Are you positive that your view is being rendered with
RequestContext
?For example:
The view above will not use the context processors listed in
TEMPLATE_CONTEXT_PROCESSORS
. Make sure you are supplying aRequestContext
like so:根据 django 文档 您可以使用
render
作为快捷方式,而不是带有 context_instance 参数的 render_to_response:According to the django docs you can use
render
as a shortcut instead of render_to_response with the context_instance argument:假设您有这样的文件结构:
1) 在任何位置创建一个 context_processors.py 文件
我将在项目文件夹中创建一个(使用 settings.py):
2) 创建一个context_processors.py 中接受 HttpRequest 对象作为参数并返回字典的函数
上下文处理器只是一个接受 HttpRequest 对象作为参数并返回字典的函数。
像这样:
3) 将其添加到
settings.py
中的context_processors
设置(出于安全原因位于底部)现在,您将能够访问整个网站上每个 django 模板上的“site_email”模板变量。
祝您编码愉快!
Let's say you have a file structure like this:
1) Anywhere, create a context_processors.py file
I'll create one in the project folder (with settings.py):
2) Create a function in context_processors.py that accepts a HttpRequest object as an argument and returns a dictionary
A context processor is just a function that accepts an HttpRequest object as an argument and returns a dictionary.
Like this:
3) Add this to your
context_processors
setting insettings.py
(at the bottom for security reasons)Now you'll be able to access the 'site_email' template variable on every single django template across your whole site.
Happy coding!
从 Django 1.8 开始,您可以像这样注册自定义上下文处理器:
假设您的上下文处理器位于应用程序
www
的context_processors.py
中Since Django 1.8 you register your custom context processors like this:
assuming your context processor is in app
www
incontext_processors.py
如果您使用 Django 的
render_to_response()
快捷方式用字典的内容填充模板,则默认情况下将向您的模板传递一个 Context 实例(而不是RequestContext
) 。要在模板渲染中使用RequestContext
,请使用render()
快捷方式,该快捷方式与使用render_to_response()
进行调用相同code>context_instance 参数强制使用RequestContext
。If you’re using Django’s
render_to_response()
shortcut to populate a template with the contents of a dictionary, your template will be passed a Context instance by default (not aRequestContext
). To use aRequestContext
in your template rendering, use therender()
shortcut which is the same as a call torender_to_response()
with acontext_instance
argument that forces the use of aRequestContext
.