无法使用豁免网址

时间:2018-09-26 11:41:43

标签: python django

嗨,我正在尝试免除我项目中的URL,以便用户可以不登录而绕过中间件并重置密码。但是,我在LOGIN_EXEMPT_URLS中放置的URL似乎无法解决此问题,而是链接重置密码会将用户重定向到帐户/登录。

settings.py:

LOGIN_EXEMPT_URLS = {

r'^account/logout/$',
r'^account/register/$',
r'^account/reset-password$',
r'^account/reset-password/done/$',
r'^account/reset-password/confirm(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,23})/$',
r'^account/reset-password/complete/$',

}

middleware.py:

url_is_exempt = any(url.match(path)for url in EXEMPT_URLS)

    if path == reverse('accounts:logout').lstrip('/'):
        logout(request)

    if request.user.is_authenticated and url_is_exempt:
        return redirect(settings.LOGIN_REDIRECT_URL)

    elif request.user.is_authenticated or url_is_exempt:
        return None

    else:
        return redirect(settings.LOGIN_URL)

urls.py:

urlpatterns = [
url(r'^$', views.home),
url(r'^login/$', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='login'),
url(r'^logout/$', auth_views.LogoutView.as_view(template_name='accounts/logout.html'), name='logout'),
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(template_name='accounts/reset_password.html',
                              success_url=reverse_lazy('accounts:password_reset_done')), 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'),
]

main / urls.py:

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


]

html:位于{%else%}中(已通过user.is.authenticated)

<ul class="navbar-nav ml-auto">
        <li class='nav-item'>
            <a class="nav-link" href='{% url 'accounts:reset_password' %}'>Forgotten Password?</a>

所有html格式都正确,但我认为某些代码无法正常运行。抱歉

仅供参考:注册豁免作品!

那是什么问题呢? thnx

1 个答案:

答案 0 :(得分:1)

您的免税正则表达式r'^account/reset-password$'不带斜杠。这与URL模式r'^reset-password/$'中的正则表达式不一致,后者确实带有斜杠。