How to specify the login_required redirect url in django?

Viewed 101581

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?

7 Answers

In django project settings

add below code

LOGIN_REDIRECT_URL = 'path/to/url'

and then import this LOGIN_REDIRECT_URL in your views and add

`@login_required(login_url=LOGIN_REDIRECT_URL)`

to the top of your views you want to restrict it will work

you can also take url from view

for example

path('login/', login_view, name='login_name'),

then decoratorwill be

@login_required(login_url='login_name')
Related