You want to use context_instance and RequestContext s.
First add views.py to the top of the page:
from django.template import RequestContext
Then refresh all your views to look like this:
def someview(request, ...) ... return render_to_response('viewtemplate.html', someContext, context_instance=RequestContext(request))
In settings.py add:
TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.auth', ... 'myproj.app.context_processors.dynamic', 'myproj.app.context_processors.sidebar', 'myproj.app.context_processors.etc', )
Each of these context_processors is a function that takes a request object and returns a context in the form of a dictionary. Just put all the functions in context_processors.py inside the corresponding application. For example, a blog may have a sidebar with a list of recent posts and comments. context_processors.py will simply define:
def sidebar(request): recent_entry_list = Entry.objects... recent_comment_list = Comment.objects... return {'recent_entry_list': recent_entry_list, 'recent_comment_list': recent_comment_list}
You can add as much or less as you want.
See the Django Template Docs for more details.
tghw
source share