Requestcontext and context processors
When rendering a template, you need a context. This can be an instance of django.template.Context
, but Django also comes with a subclass, django.template.RequestContext
, that acts slightly differently.
RequestContext
adds a bunch of variables to your template context by default-things like the HttpRequest
object or information about the currently logged-in user.
The render()
shortcut creates a RequestContext
unless it's passed a different context instance explicitly. For example, consider these two views:
from django.template import loader, Context def view_1(request): # ... t = loader.get_template('template1.html') c = Context({ 'app': 'My app', 'user': request.user, 'ip_address': request.META['REMOTE_ADDR'], ...
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.