我正在尝试在我的settiings.py文件中引用一个URL。 我目前在urls.py文件中
from django.urls import (
path,
reverse_lazy,
re_path
)
my_app = 'accounts'
re_path(
r'^reset_password_confirm/(?P<uidb64>[0-9A-Za-z_-]+)-(?P<token>.+)/$',
PasswordResetConfirmView.as_view(
success_url = reverse_lazy('accounts:password_reset_complete')
),
name = 'password_reset_confirm',
),
并且我的settings.py文件中有这个
LOGIN_EXEMPT_URLS =(
'accounts/login/',
'accounts/register/',
'accounts/',
'accounts/reset_password/',
'accounts/reset_password_done/',
r'^accounts/reset_password_confirm/(?P<uidb64>[0-9A-Za-z_-]+)-(?P<token>.+)/$',
'accounts/password_reset_complete/',
)
这是我的自定义登录中间件
from django.conf import settings
from django.shortcuts import redirect,render
from django.urls import reverse
EXEMPT_URLS = settings.LOGIN_EXEMPT_URLS
class LoginRequired:
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')
path = request.path.lstrip('/')
url_is_exempt = any(path==url for url in EXEMPT_URLS)
if request.user.is_authenticated and url_is_exempt:
if path == reverse('accounts:homepage').lstrip('/'):
return None
return redirect(settings.LOGIN_REDIRECT_URL)
elif request.user.is_authenticated or url_is_exempt:
return None
else:
return redirect(settings.LOGIN_REDIRECT_URL)
我认为我的主要问题是
"r'^accounts/reset_password_confirm/(?p<uidb64>[0-9A-Za-z_-]+)-(?p<token>.+)/$'"
因为其他人似乎都可以工作
答案 0 :(得分:0)
我想我终于明白了。问题出在自定义登录中间件上 进程视图功能。我应该已经导入了're'模块,而是这样做了
from django.conf import settings
from django.shortcuts import redirect,render
from django.urls import reverse
import re
EXEMPT_URLS = [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS]
class LoginRequired:
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')
path = request.path_info.lstrip('/')
url_is_exempt = any(url.match(path) for url in EXEMPT_URLS)
if request.user.is_authenticated and url_is_exempt:
if path == reverse('accounts:homepage').lstrip('/'):
return None
return redirect(settings.LOGIN_REDIRECT_URL)
elif request.user.is_authenticated or url_is_exempt:
return None
else:
return redirect(settings.LOGIN_REDIRECT_URL).
主要方面是
EXEMPT_URLS = [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS]
和
url_is_exempt = any(url.match(path) for url in EXEMPT_URLS)
有了这个我就可以匹配网址