75

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?

1
  • Is there anything like logout_required in django?
    – Syed Ali
    Feb 9, 2019 at 14:30

10 Answers 10

106

LOGIN_URL in your settings

Reference:

78

you can do this in your view works fine for me without declaring in settings.py

from django.contrib.auth.decorators import login_required

@login_required(login_url='/example url you want redirect/') #redirect when user is not logged in
def myview(request):
    do something
    return something #returns when user is logged in
3
  • 7
    This is correct and worked perfectly but always prefer using LOGIN_URL because of login url changed in future you may need to change lots of places
    – Yash
    Jan 22, 2017 at 16:24
  • 2
    Its better to use the url name instead of the path as it can change anytime.re_path(r"login$", LoginView.as_view(template_name="registration/login_form.html"), name="player_login") then the decorator is @login_required(login_url="player_login") Ofcourse it superseeds the login_url and login_url_redirect in settings. Thanks for sharing.
    – Doogle
    Oct 14, 2018 at 6:20
  • It would have been better if you'd have added where to import login_required from. Nov 22, 2018 at 19:38
16

default login url is: '/accounts/login/'
if you want to change it then go to settings.py

LOGIN_URL='/path/to/url'
LOGIN_REDIRECT_URL='/path/to/redirecturl'
10

this from documentation should be helpful: https://docs.djangoproject.com/en/1.5/topics/auth/default/#the-login-required-decorator

@login_required(login_url='/accounts/login/')
def my_view(request):
    ...
1
  • Let say I tried to go to mysite/home from a non-authenticated user, this login_url tries to get to the login page from mysite/home/login_user.html. The problem with this is that it keeps the /home/ path and so does not work. Any idea?
    – Robin
    May 3, 2022 at 19:44
3

Go to your setting.py You can add this anywhere in your settings.py file but i prefer to place it at the bottom. LOGIN_URL = '/login/'

NOTE: '/login/' is the URL segment that brings the user to the login page. The complete URL is similar to this "myexample.com/login/".

2

We have two approaches, first is the best practice for code maintenance in the future, and the second will be a hassle when the URL for login changes

First approach

in setting.py import reverse_lazy, and set LOGIN_URL to the login namespace

from django.urls import reverse_lazy
LOGIN_URL = reverse_lazy('login')

In your views, you import login_required and require login before each function

from django.contrib.auth.decorators import login_required

@login_required()
def view_name(request):
    pass

Second approach
This approach is not reliable since you won't have a defined variable holding your login URL namespace

In your views, you import login_required and require login before each function

from django.contrib.auth.decorators import login_required

@login_required(login_url='/path/to/login/')
def view_name(request):
    pass

In both cases what is static is you will have to import login_required in views

1

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')
3
  • I tried this and it didn't work for me
    – Robin
    May 3, 2022 at 19:41
  • if you want to use the name attribute, you have to use the django.urls.reverse function, i.e. @login_required(login_url=reverse('login_name'))
    – Fabian
    Aug 31, 2022 at 21:50
  • ERRATUM: Don't use the reverse in the @login_required() or you'll lose an hour debugging :( It generates a "circular" issue and crashes (i.e. urls are not recognised anymore).
    – Fabian
    Aug 31, 2022 at 22:36
1

Django 4+ makes this very easy.

In urls.py make sure you have a path with a name you can reference:

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

Then in settings.py, all you need to do is add this to the bottom:

LOGIN_URL = 'login_name'

Now all your @login_required decorators will automatically send anyone not logged in to your login page. If you change that url in the future, the settings.py file stays updated since it's referencing the name and not the absolute url path.

I recommend against doing any of the 'path/to/url' approaches above since as soon as you change that path you then need to change everything. Reference the 'login_name' once in your settings.py file, and be done forever.

0

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

0

My signin/signup page was ignoring the ?next=... part of my path, and just redirecting to the home page, '/'.

By including: <input type="hidden" name="next" value="{{ request.GET.next }}">

After my submit button, the redirect worked properly.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.