Issue
I develop Django web app using Django authentification backend. It works without having to define any views or forms, only template to 'bootstrap' it.
But I would like to change 'navigation' url and probably 'redesign' template using django-crispy-form.
But the first step is make user directly access Login page when the 'root' url (https://<my_domain>/) is enter in the navigation adress bar.
Currently, user access home page of my web app that contain a login button that redirect to https://<my_domain>/registration/login
Do I need to override all authentification views (and forms for design) and change url as needed?
Or is there an easiest way, maybe using settings.py to make user redirect to login page from root url?
project
- app
- core
- settings
- base.py
- ...
- views.py
- urls.py
- app1
- forms.py
- views.py
- templates
- app1
- registration # <= currently no forms.py nor views.py
- templates
- registration
- login.html
- password_change_done.html
- ...
- static
- templates
- layout
- base.html
- home.html
core/urls.py
urlpatterns = [
path('', views.home, name='home'), # <= 'root' url
# path('registration/', include('registration.urls')), # <= registration views override
path('registration/', include('django.contrib.auth.urls')),
]
core/settings.py
LOGIN_URL = 'home'
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'login'
Solution
You cannot have that:
LOGIN_URL = 'home'
Use classic solution:
LOGIN_URL = 'login'
And then use decorator mentioned by @AnkitTiwari if you operate in Function Based Views
just on top on home
view:
@login_required
def home_view(request):
Or LoginRequiredMixin
in Class Based Views
:
from django.contrib.auth.mixins import LoginRequiredMixin
class HomeView(LoginRequiredMixin, View):
Answered By - NixonSparrow
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.