gpt4 book ai didi

Django 1.4 : Why is my context processor not working?

转载 作者:行者123 更新时间:2023-12-04 17:11:17 29 4
gpt4 key购买 nike

我最近将一个项目从 Django 1.3 升级到 1.4,这似乎破坏了我的上下文处理器。

myapp/myapp/processors.py :

def currentPath(request):
return {'current_path': request.get_full_path()}

myapp/myapp/settings.py :
from django.conf import global_settings

global_settings.TEMPLATE_CONTEXT_PROCESSORS += (
'myapp.processors.currentPath',
'django.core.context_processors.request',
)

在任何模板中, {{ current_path }}预期 - 并且确实,直到升级,返回当前路径。现在,它根本没有得到处理。我完全被困在这里。

最佳答案

Django 1.8将处理器添加到 TEMPLATE_CONTEXT_PROCESSORS不会工作。

Deprecated since version 1.8:
Set the 'context_processors' option in the OPTIONS of a DjangoTemplates backend instead.

你必须这样做:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
],
'OPTIONS': {
'context_processors': [
# Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
# list if you haven't customized them:
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
'loaders': [
# insert your TEMPLATE_LOADERS here
]
},
},
]

关于Django 1.4 : Why is my context processor not working?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12026689/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com