我想知道如何将变量传递给我的所有模板,而无需在views.py文件中的每个方法上重复相同的代码?
在下面的示例中,我想使类别(类别对象的数组)可用于Web应用程序中的所有模板。
Eg: I would like to avoid writing 'categories':categories on every method. Is it possible?
一种查看方法
def front_page(request):
categories = Category.objects.all()
if is_logged_in(request) is False:
return render_to_response('users/signup.html', {'is_logged_in': is_logged_in(request), 'categories':categories}, context_instance=RequestContext(request))
else:
return render_to_response('users/front_page.html', {'is_logged_in': is_logged_in(request), 'categories':categories},context_instance=RequestContext(request))
另一种查看方法
def another_view_method(request):
categories = Category.objects.all()
return render_to_response('eg/front_page.html', {'is_logged_in': is_logged_in(request), 'categories':categories},context_instance=RequestContext(request))
CONTEXT PROCESSORS