我使用Django身份验证后端开发Django web应用程序。它的工作不需要定义任何视图或表单,只需要模板来"引导"它。
但我想更改"导航"url,并可能使用django脆格式"重新设计"模板。
但第一步是当在导航地址栏中输入"root"url(https://<my_domain>/(时,让用户直接访问登录页面。
目前,用户访问我的web应用程序的主页,该主页包含重定向到https://<my_domain>注册/登录
我需要覆盖所有身份验证视图(和设计表单(并根据需要更改url吗?
或者有没有一种最简单的方法,可以使用settings.py让用户从根url重定向到登录页面?
项目
- 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/ulls.py
urlpatterns = [
path('', views.home, name='home'), # <= 'root' url
# path('registration/', include('registration.urls')), # <= registration views override
path('registration/', include('django.contrib.auth.urls')),
]
核心/设置.py
LOGIN_URL = 'home'
LOGIN_REDIRECT_URL = 'home'
LOGOUT_REDIRECT_URL = 'login'
您不能拥有:
LOGIN_URL = 'home'
使用经典解决方案:
LOGIN_URL = 'login'
然后使用@AnkitTiwari提到的装饰器,如果你在Function Based Views
中操作,就在home
视图的顶部:
@login_required
def home_view(request):
或Class Based Views
:中的LoginRequiredMixin
from django.contrib.auth.mixins import LoginRequiredMixin
class HomeView(LoginRequiredMixin, View):