django Anfrage in Vorlage
Frage
Ich habe den django Anforderungsprozessor aktiviert
TEMPLATE_PROCESSORS = (
"django.core.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.request",
)
Noch muss ich Variablen in Vorlagen nicht anfordern. Ich habe manuell es passieren. Mit django 1.0.2 Überall auf Web-es scheint, es ist nur über Anforderungsprozessor aktiviert ..
Auch verwende ich Request wie:
return render_to_response(
'profile.html',
{
'persons':Person.objects.all(),
'person':Person.objects.get(id=id),
'request':request,
},
context_instance=RequestContext(request)
)
kein Glück
ohh darn
der neue Name für das heißt TEMPLATE_CONTEXT_PROCESSORS
Lösung
settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
# ...
'django.core.context_processors.request',
# ...
)
Andere Tipps
TEMPLATE_CONTEXT_PROCESSORS Anstatt von TEMPLATE_PROCESSORS
Beachten Sie, dass wie von Django 1.8, dies zu einer „templates“ Einstellung geändert hat, und in der Standardkonfiguration wird der Anforderungsprozessor NICHT enthalten.
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# insert your TEMPLATE_DIRS here
],
'APP_DIRS': True,
'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',
],
},
},]
Fügen Sie einfach den Anforderungsprozessor zurück in das Problem zu beheben:
'django.core.context_processors.request',
Weitere Informationen finden Sie in den Django Upgrade Docs .
Sind Sie sicher, dass Sie nicht über die request
Variable auf die Vorlage zur Verfügung haben? Was passiert, wenn Sie die Zeile
'request':request,
Das ist anders, wenn diese Zeile vorhanden ist. Wenn Sie Ihre Vorlage die gleiche Art und Weise entweder lädt, das Problem mit der Vorlage ist.
MIDDLEWARE_CLASSES = ( ... 'Yourfolder.yourfile.yourclass', ... Yourclass:
Klasse AddRequestToTemplate: process_templaet_response (self, request, response): response.context_data [ 'request'] = Anfrage