因为显示需要,例如用户等信息是需要在每次请求传递回页面!
django传递公共的参数到界面有两种方法
1.中间件
修改settings.py配置信息 MIDDLEWARE_CLASSES 增加自定义的中间件
中间件的缺点是render_to_response无法触发process_template_response
必须返回带有render()方法的HttpResponse()的实例
class PublicParameter(object):
def process_template_response(self, request, response):
"""
传递公共数据
"""
user = User.objects.get()
if isinstance(response.context_data, dict):
response.context_data['user'] = projectList
else:
response.context_data = {'user':projectList}
return response
2.通过RequestContext对象
配置settings.py中 TEMPLATE_CONTEXT_PROCESSORS
TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth