20π
An Alternative to
if user.is_anonymous():
# user is anon user
is by testing to see what the id of the user object is:
if user.id == None:
# user is anon user
else:
# user is a real user
see https://docs.djangoproject.com/en/dev/ref/contrib/auth/#anonymous-users
- [Django]-Django Sitemaps and "normal" views
- [Django]-(13: Permission denied) while connecting to upstream:[nginx]
- [Django]-Homepage login form Django
10π
You should check the value of request.user.is_authenticated
. It will return True
for a User
instance and False
for an AnonymousUser
instance.
One answer suggests using is_anonymous
but the django.contrib.auth documentation says βyou should prefer using is_authenticated
to is_anonymous
β.
- [Django]-How can I change the default Django date template format?
- [Django]-Google Static Maps URL length limit
- [Django]-Django-allauth: Linking multiple social accounts to a single user
4π
I know Iβm doing a bit of grave digging here, but a Google search brought me to this page.
If your view def requires that the user is logged in, you can implement the @login_required decorator:
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
β¦
- [Django]-Django: How to check if the user left all fields blank (or to initial values)?
- [Django]-On Heroku, is there danger in a Django syncdb / South migrate after the instance has already restarted with changed model code?
- [Django]-Stack trace from manage.py runserver not appearing
4π
I had a similar issue except this was on a page that the login_redirect_url was sent to. I had to put in the template:
{% if user.is_authenticated %}
Welcome Back, {{ username }}
{% endif %}
- [Django]-Using the reserved word "class" as field name in Django and Django REST Framework
- [Django]-How to move a model between two Django apps (Django 1.7)
- [Django]-How does django handle multiple memcached servers?
3π
In Django rest framework itβs good idea to use permission classes to check if user is authenticated. This will apply to whole viewSet. In the simpliest form it could look like this:
from rest_framework.permissions import IsAuthenticated
...
class SomeViewSet(viewsets.GenericViewSet):
permission_classes = [IsAuthenticated]
- [Django]-Sending HTML email in django
- [Django]-Django dump data for a single model?
- [Django]-Django Model Field Default Based Off Another Field in Same Model
0π
You should use request.user.is_authenticated
as it is the preferred solution over the request.user.is_anonymous
(the accepted answer)
- [Django]-How to implement followers/following in Django
- [Django]-.filter() vs .get() for single object? (Django)
- [Django]-How to reload modules in django shell?
0π
You can use is_authenticated and is_anonymous with HttpRequest.user and get_user(request) to check if the user is anonymous or not in Django Views as shown below. *The doc recommends is_authenticated
instead of is_anonymous
:
# "views.py"
from django.shortcuts import render,
from django.contrib.auth import get_user
def test(request):
print(request.user.is_authenticated) # True or False
print(request.user.is_anonymous) # True or False
print(get_user(request).is_authenticated) # True or False
print(get_user(request).is_anonymous) # True or False
return render(request, 'index.html')
And, you can check if the user is anonymous or not in Django Templates as shown below:
{% "index.html" %}
{{ user.is_authenticated }} {% True or False %}
{{ user.is_anonymous }} {% True or False %}
- [Django]-MySQL "incorrect string value" error when save unicode string in Django
- [Django]-What is the best django model field to use to represent a US dollar amount?
- [Django]-How do I run tests for all my Django apps only?