在django中创建我自己的上下文处理器

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)

  • @TM值.`return render_to_response('template.html',context_instance = RequestContext(request))`是旧的fasion,我认为`return render(request,'template.html')`更干 (3认同)
  • Django 的内置视图会为您处理这个问题(它们使用 `RequestContext`)。想想你制作的上下文处理器。它以 `request` 作为参数。这意味着您需要以某种方式将当前请求提供给渲染逻辑。`RequestContext` 基本上只是处理循环所有上下文处理器并将当前请求传递给它们的简单逻辑,然后用结果更新页面上下文。 (2认同)

bpe*_*tit 29

根据django文档,您可以使用rendercontext_instance参数作为捷径而不是render_to_response:

或者,使用与render()render_to_response()调用相同的快捷方式,以及强制使用RequestContext的context_instance参数.


Zac*_*ché 22

假设您有这样的文件结构:

\n
YourDjangoProject\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\n
Run Code Online (Sandbox Code Playgroud)\n

1)在任何地方,创建一个 context_processors.py 文件

\n

我将在项目文件夹中创建一个(使用 settings.py):

\n
YourDjangoProject\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\n
Run Code Online (Sandbox Code Playgroud)\n

2) 在 context_processors.py 中创建一个函数,该函数接受 HttpRequest 对象作为参数并返回一个字典

\n

上下文处理器只是一个接受 HttpRequest 对象作为参数并返回字典的函数。

\n

像这样:

\n
# project/context_processors.py\n\ndef site_email(request):\n    return {'site_email': 'example@gmail.com'}\n
Run Code Online (Sandbox Code Playgroud)\n

3)将此添加到您的context_processors设置中settings.py(出于安全原因位于底部)

\n
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]\n
Run Code Online (Sandbox Code Playgroud)\n

现在,您将能够访问整个站点上每个 django 模板上的“site_email”模板变量。

\n

快乐编码!

\n


and*_*abs 9

从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 wwwcontext_processors.py