Django context processor gets AnonymousUser
instead of User.
def myview(request):
return render_to_response('tmpl.html', {'user': User.objects.get(id=1})
works fine and passes User to template. But
def myview(request):
return render_to_response('tmpl.html', {}, context_instance=RequestContext(request))
开发者_StackOverflowwith a context processor
def user(request):
from django.contrib.auth.models import User
return {'user': User.objects.get(id=1)}
passes AnonymousUser, so I can't get the variables I need :( What's wrong?
Are you sure that your context processor is enabled in TEMPLATE_CONTEXT_PROCESSORS
in settings.py?
More to the point, does it come before or after the built-in django.contrib.auth.context_processors.auth
? If it's before, it will be overridden by that processor, which will redefine user
as the actual logged-in user, or AnonymousUser if none.
精彩评论