[Answer]-Use named URLs in @login_required decorator

1👍

You can specify the URL users not logged in will be redirected to by setting LOGIN_URL in your settings.py globally. (Of course you can also override that setting where ever you apply the decorator if you want to.)

According to the docs, you can also specify named url patterns, e.g., LOGIN_URL = 'myapp:login', and view function names, e.g., LOGIN_URL = 'myapp.login_view'.

From the docs:

LOGIN_URL

Default: '/accounts/login/'

The URL where requests are redirected for login, especially when using the login_required() decorator.

This setting also accepts view function names and named URL patterns which can be used to reduce configuration duplication since you don’t have to define the URL in two places (settings and URLconf).

For reference:

0👍

You can set the login URL of your site in your settings.py file as LOGIN_URL. Then, you don’t have to worry about changing it in the different places.

Leave a comment