所以我是Django的新手,我正在创建一个名为accounts的webapp。我希望在我的urlpatterns中有一个路径方法,每当我输入我的其他路径方法中没有提到的url时,我就会将其发送到登录页面。(注意:我使用了内置的登录方法)如何实现?
URLs.朋友
from django.urls import path, url
from . import views
from django.contrib.auth.views import login, logout
urlpatterns = [
path('display/', views.display),
path('', views.home),
path('login/', login, {'template_name': 'accounts/login.html'}),
path('logout/', logout, {'template_name': 'accounts/logout.html'}),
path('register/',views.register, name='register'),
url('xxxx', login, {'template_name': 'accounts/login.html'}) #here, I'd like to change 'xxxx'
]
您只需要在views.py
中创建一个将处理404的条目:
from django.shortcuts import render_to_response
from django.template import RequestContext
def handler404(request):
response = render_to_response('your_login_template.html', {},
context_instance=RequestContext(request))
response.status_code = 404
return response
然后在你的urls.py
中你可以指定它:
from django.conf.urls.defaults import handler404, handler500
handler404 = 'views.handler404'
请注意,这只适用于DEBUG=False
中的settings.py
。