2009-03-31 24 views
19

Ich habe die django Anforderungsprozessordjango Anfrage in Vorlage

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", 
) 

Weiterhin aktiviert ich muss in Vorlagen Variable zur Verfügung nicht anfordern. Ich muss es manuell übergeben. Mit django 1.0.2 überall auf Internet über es scheint es nur Anforderungsprozessor aktiviert ..

Auch ich bin mit Request als:

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 die neuen Namen für das heißt TEMPLATE_CONTEXT_PROCESSORS

+0

Warum lassen Sie die Vorlage etwas über die Anfrage wissen? – yfeldblum

+2

easy: Ich muss den Pfad * URL * der Anfrage für dynamische Navigation kennen. Ich bin fertig, es funktioniert jetzt. – Attila

Antwort

39

settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    # ... 
    'django.core.context_processors.request', 
    # ... 
) 
+0

Dank Dingo, ich fount ein Tutorial hier http://wiki.workassis.com/django-accessing-request-object-in-template/ –

+2

In späteren Versionen von Djano, verwenden Sie stattdessen: 'django.template.context_processors.request' – shacker

10

TEMPLATE_CONTEXT_PROCESSORS statt TEMPLATE_PROCESSORS

0

Sind Sie sicher, dass die Variable request nicht für die Vorlage verfügbar ist? Was passiert, wenn Sie die Zeile entfernen

das ist anders als wenn diese Zeile vorhanden ist. Wenn Ihre Vorlage auf die gleiche Weise geladen wird, liegt das Problem bei Ihrer Vorlage.

+0

Für mich war es, dass die Funktion Verarbeitung der Anfrage und Update-Kontext, Kontext ohne die Anfrage übergeben. Die Anfrage ging verloren und musste manuell an die Funktion übergeben werden, da es im Gegensatz zu render() nicht schlau war, die Anfrage zu übergeben. Etwas mit dieser Antwort verbunden. – radtek

0

MIDDLEWARE_CLASSES = ( ... 'yourfolder.yourfile.yourclass', ... Yourclass:

Klasse AddRequestToTemplate: process_templaet_response (self, Anfrage, Antwort): response.context_data ['Anfrage'] = Anfrage

4

Seien Sie darauf hingewiesen, dass ab Django 1.8 Dies hat sich zu einer "TEMPLATES" -Einstellung geändert, und in der Standardkonfiguration ist der Anfrageprozessor 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', 
     ], 
    }, 
},] 

einfach den Anforderungsprozessor hinzufügen zurück in das Problem zu beheben:

'django.core.context_processors.request', 

Weitere Informationen finden Sie in der Django Upgrading Docs.

+0

Sind Sie sicher, dass es standardmäßig in 1.8 deaktiviert ist? https://github.com/django/django/blob/1.8.6/django/conf/project_template/project_name/settings.py#L63 Ich habe am 1.9.6 getestet mit 'django-admin startproject' und es war dort auch https://github.com/django/django/blob/1.9.6/django/conf/project_template/project_name/settings.py-tpl # L63 –

+0

Zuletzt neues Projekt, das ich eingerichtet habe, es fehlte, aber ich könnte mich irren .... – MontyThreeCard