I have a view function:
@login_required def myview(): # do something # respond something pass
How can I specify the exact URL for this view function to be redirected?
When you are not logged-in and request a URL that requires authentication, like the Django admin, Django redirects you to the login page. When you log in successfully, Django redirects you to the URL you requested originally.
This kind of redirect is called an open or unvalidated redirect. There are legitimate use cases for redirecting to URL that is read from user input. A prime example is Django’s login view. It accepts a URL parameter next that contains the URL of the page the user is redirected to after login.
Let’s see how. When you try to access a page of Django admin without authentication, it redirects you to the log-in page. There when you input the right credentials, it redirects you to the same page you wanted to access before the log-in page.
They do not need to send any information, so to log out they should just go to the right URL. In urlpatterns you can find that for logout we use LogoutView class from django.contrib.auth.views module. Just import it to the urls.py module to complete the work.
LOGIN_URL in your settings
Reference:
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