简体   繁体   中英

How can i check if the user is authenticated or not in base.html in django?

If the user anonymous or not, how can i check that in base.html?

Which is correct?

{% if request.user.is_authenticated %}

Or

{% if user.is_authenticated %}

How can i pass the variable request.user in base.html?

Try using the Request context:

from django.template import RequestContext

# in a view
return render_to_response('base.html', context_instance = RequestContext(request))

Make sure you take a look at these context processors: https://docs.djangoproject.com/en/dev/ref/templates/api/#django-contrib-auth-context-processors-auth

And then you should be able to access user in your templates

You can use the django shortcut render to always render a template that is automatically passed a RequestContent:

https://docs.djangoproject.com/en/dev/topics/http/shortcuts/#module-django.shortcuts

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM