[Django]-Allow access to only non-logged in user in django

4👍

look at the user_passes_test decorator.

You would do something like:

from django.contrib.auth.decorators import user_passes_test
@user_passes_test(lambda u: not u.is_authenticated())
def my_view(request):
    ...

user_passes_test receives a function. Where that function receives the User object. That function must return a bool to determine whether or not the view should be executed.

If you’re not using the User object to authenticate users, then you would have to write your own decorator.

Leave a comment