dot*_*tty 75 django views django-context
我已经到了一个点,我需要将某些变量传递给我的所有视图(主要是自定义身份验证类型变量).
我被告知写我自己的上下文处理器是最好的方法,但我有一些问题.
我的设置文件如下所示
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",
)
Run Code Online (Sandbox Code Playgroud)
如您所见,我有一个名为'context_processors'的模块和一个名为'say_hello'的函数.
看起来像
def say_hello(request):
return {
'say_hello':"Hello",
}
Run Code Online (Sandbox Code Playgroud)
我是否正确地假设我现在可以在我的观点中做以下事情?
{{ say_hello }}
Run Code Online (Sandbox Code Playgroud)
现在,这在我的模板中没有任何内容.
我的观点看起来像
from django.shortcuts import render_to_response
def test(request):
return render_to_response("test.html")
Run Code Online (Sandbox Code Playgroud)
TM.*_*TM. 49
您编写的上下文处理器应该可以工作.问题出在您看来.
您是否认为您的观点正在呈现RequestContext?
例如:
def test_view(request):
return render_to_response('template.html')
Run Code Online (Sandbox Code Playgroud)
上面的视图不会使用上面列出的上下文处理器TEMPLATE_CONTEXT_PROCESSORS.确保你提供的RequestContext是这样的:
def test_view(request):
return render_to_response('template.html', context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)
Zac*_*ché 22
假设您有这样的文件结构:
\nYourDjangoProject\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80project\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80__init__.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80asgi.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80settings.py\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80urls.py\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80wsgi.py\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80.env\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80manage.py\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80db.sqlite3\nRun Code Online (Sandbox Code Playgroud)\n1)在任何地方,创建一个 context_processors.py 文件
\n我将在项目文件夹中创建一个(使用 settings.py):
\nYourDjangoProject\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80project\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80...\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80context_processors.py\nRun Code Online (Sandbox Code Playgroud)\n2) 在 context_processors.py 中创建一个函数,该函数接受 HttpRequest 对象作为参数并返回一个字典
\n上下文处理器只是一个接受 HttpRequest 对象作为参数并返回字典的函数。
\n像这样:
\n# project/context_processors.py\n\ndef site_email(request):\n return {'site_email': 'example@gmail.com'}\nRun Code Online (Sandbox Code Playgroud)\n3)将此添加到您的context_processors设置中settings.py(出于安全原因位于底部)
TEMPLATES = [\n {\n 'BACKEND': 'django.template.backends.django.DjangoTemplates',\n 'DIRS': [BASE_DIR / 'config' / 'templates'],\n 'APP_DIRS': True,\n 'OPTIONS': {\n 'context_processors': [\n 'django.template.context_processors.debug',\n 'django.template.context_processors.request',\n 'django.contrib.auth.context_processors.auth',\n 'django.contrib.messages.context_processors.messages',\n 'project.context_processors.site_email', # <- New context processor here\n ],\n },\n },\n]\nRun Code Online (Sandbox Code Playgroud)\n现在,您将能够访问整个站点上每个 django 模板上的“site_email”模板变量。
\n快乐编码!
\n从Django 1.8开始,您可以注册自定义上下文处理器,如下所示:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
'templates'
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'www.context_processors.instance',
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
假设你的上下文处理器在app www中context_processors.py
| 归档时间: |
|
| 查看次数: |
35265 次 |
| 最近记录: |