Search code examples
pythondjangoauthenticationdjango-registration

how to set django root url as login url?


when using django-registration , the url confuse me a bit

say i want my inidex page localhost:8000 to be a login page, but the django-registration's login url has a /login prefix, do i have to put the /login in my url like

url(r'^/login/$', include('registration.backends.default.urls')),

in this case , the localhost:8000 will be empty, dose it mean after deploy this project ,view the url like something.com will be empty.

how to set the index page to be a actual django-registration login page


Solution

  • No not really, you need to include the @login_required decorator on the views that you want the user to be authenticated, for eg.

    # views.py
    
    from django.contrib.auth.decorators import login_required
    from django.http import HttpResponse
    
    @login_required
    def index(request):
        return HttpResponse('Only registers will see this page')
    
    # now you can add the stuff that you would want only the registered users to view, 
    # so basically if a new visitors tries to visit your site say at www.abc.com, he will first be redirected to the www.abc.com/login/ where he needs to create an account, once authenticated, he will then be redirected to the index page
    

    Reference django-registration, registration and login form on index page together, did I do it right?