Vote count:
0
I am having trouble getting RequestContext to work. (Django 1.7.6)
I need this so that I can render certain variables to the base.html template (which every template extends) and I am tired of passing the same dictionaries to render() from every view.
I am using render() as per this note from the docs:
To use a RequestContext in your template rendering, use the render() shortcut which is the same as a call to render_to_response() with a context_instance argument that forces the use of a RequestContext.
Please note that I'm not just doing this to get MEDIA_URL, the default context processors in django 1.7 already pass the media url automatically. I would like to do this for things that render on every single page such as the "currently active button", or "see this random picture from the gallery", or "partner links" on the side-bar, etc.
My code is sensitive so I made a new app to show this error. In my settings.py I have:
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.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"testrequestcontext.context_processors.display_media_url",
)
Context_processors.py:
def media_url(request):
from rmlsa import settings
return {'MEDIA_URL': settings.MEDIA_URL}
Views.py:
def display_media_url(request):
return render(request, 'testrequestcontext/display_media_url.html')
Template:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p>{{ MEDIA_URL }}</p>
</body>
</html>
Error:
ImportError at /testrequestcontext/ Module "testrequestcontext.context_processors.display_media_url" does not define a "display_media_url" attribute/class
Django ImportError using RequestContext
Aucun commentaire:
Enregistrer un commentaire