(另一个)自定义模板标签在DEBUG = False时引发KeyError

时间:2014-12-16 11:28:10

标签: django django-templates

我正在尝试使用自定义模板标签(实际上这个:https://djangosnippets.org/snippets/2875/),在我的项目中,只要DEBUG = True就可以正常工作。当它为假时,相关的错误部分是:

File "...app/templatetags/helper_tags.py", line 15, in change_lang
path = context['request'].path
File "...local/lib/python2.7/site-packages/django/template/context.py",
line 56, in __getitem__
raise KeyError(key)
KeyError: 'request'

我已经看过很多像这样的问题,而且我已经完成了所有人都看到的设置:

1)views.py使用django.shortcuts.render:

from django.shortcuts import render
def home(request):
    return render(request, 'home.html')

2)settings.py包含:

ALLOWED_HOSTS = ['*']

from django.conf import global_settings
TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    "django.core.context_processors.request",
)

我还创建了一个具有相同Django版本(1.6.2)的新项目,试图尽可能地模仿有问题的项目,并且对于我的绝望,当DEBUG = False时它也能正常工作。

非常感谢任何提示。

干杯!

0 个答案:

没有答案
相关问题