当前路径account / login /与以下任何路径都不匹配?

时间:2018-09-21 14:19:02

标签: python django django-authentication

我正在尝试访问我的项目(本地),并且得到了该追溯:

回溯

Page not found (404)
Request Method:
GET
Request URL:
http://127.0.0.1:8000/account/login/
Using the URLconf defined in tutorial.urls, Django tried these URL patterns, in this order: 
[name='login_redirect'] 
admin/ 
account/ ^$ 
account/ ^login$ 
account/ ^logout$ 
account/ ^register$ [name='register'] 
account/ ^profile$ [name='view_profile'] 
account/ ^profile/edit$ [name='edit_profile'] 
account/ ^change-password$ [name='change_password'] 
account/ ^reset-password$ [name='reset_password'] 
account/ ^reset-password/done$ [name='password_reset_done'] 
account/ ^reset-password/confirm(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A- Za-z]{1,13}-[0-9A-Za-z]{1,23})/$ [name='password_reset_confirm'] 
account/ ^reset-password/complete$ [name='password_reset_complete'] 
The current path, account/login/, didn't match any of these. 

Main / urls.py

from django.contrib import admin
from django.urls import path, re_path, include
from tutorial import views

urlpatterns = [
path('', views.login_redirect, name='login_redirect'),
path('admin/', admin.site.urls),
path('account/', include('accounts.urls')),
]

project / urls.py

from django.conf.urls import url
from . import views
from django.contrib.auth import views as auth_views
from django.contrib.auth.views import (

PasswordResetView,
PasswordResetDoneView,
PasswordResetConfirmView,
PasswordResetCompleteView,

)

from django.urls import re_path


urlpatterns = [
url(r'^$', views.home),
url(r'^login$', auth_views.LoginView.as_view(template_name='accounts/login.html')),
url(r'^logout$', auth_views.LogoutView.as_view(template_name='accounts/logout.html')),
url(r'^register$', views.register, name='register'),
url(r'^profile$', views.view_profile, name='view_profile'),
url(r'^profile/edit$', views.edit_profile, name='edit_profile'),
url(r'^change-password$', views.change_password, name='change_password'),
url(r'^reset-password$', PasswordResetView.as_view(), name='reset_password'),
url(r'^reset-password/done$', PasswordResetDoneView.as_view(), name='password_reset_done'),
url(r'^reset-password/confirm(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,23})/$',
    PasswordResetConfirmView.as_view(), name='password_reset_confirm'),
url(r'^reset-password/complete$', PasswordResetCompleteView.as_view(), name='password_reset_complete'),
]

middleware.py

import re
from django.conf import settings
from django.shortcuts import redirect


class LoginRequiredMiddleware:

def __init__(self, get_response):
    self.get_response = get_response

def __call__(self, request):
    response = self.get_response(request)
    return response

def process_view(self, request, view_func, view_args, view_kwargs):
    assert hasattr(request, 'user')

    if not request.user.is_authenticated:
        if True:
            return redirect(settings.LOGIN_URL)

settings.py

LOGIN_REDIRECT_URL = '/account/login'
LOGIN_URL = '/account/login/'

这是什么问题?重定向有效,我在urlpatterns中有一种感觉,但一生都无法解决。今天让我发疯。感谢您的帮助,对本文的答复感到遗憾!

1 个答案:

答案 0 :(得分:1)

您的LOGIN_URL设置后有斜杠:

LOGIN_URL = '/account/login/'

但是您login的网址格式不允许使用斜杠。

url(r'^login$', auth_views.LoginView.as_view(template_name='accounts/login.html')),

您可以从LOGIN_URL中删除斜杠。但是,在Django中,通常的做法是在网址中添加斜杠。然后,默认行为会将带有斜杠的所有请求从/accounts/login重定向到/accounts/login/