In django how can I prevent certain pages to be displayed to the logged in user. For example login page, signup page. That is I would like to prevent logged in user to reach login and signup pages.
One way of doing it would be to write my own view for login and in that view check if user has already logged in, and if he is, redirect to some other page.
Is there a better way to achieve this? Like some decorator?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With