How to specify the login_required redirect url in django?

后端 未结 7 1264
既然无缘
既然无缘 2020-12-13 23:04

I have a view function:

@login_required
def myview():
    # do something
    # respond something
    pass

How can I specify the exact URL f

相关标签:
7条回答
  • 2020-12-13 23:36

    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 讨论(0)
  • 2020-12-13 23:49

    LOGIN_URL in your settings

    Reference:

    • LOGIN_URL
    • LOGIN_REDIRECT_URL
    0 讨论(0)
  • 2020-12-13 23:50

    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):
        ...
    
    0 讨论(0)
  • 2020-12-13 23:51

    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
    
    0 讨论(0)
  • 2020-12-13 23:58

    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'
    
    0 讨论(0)
  • 2020-12-14 00:01

    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/".

    0 讨论(0)
提交回复
热议问题