Django Middleware保持重定向,访问管理站点是不可能的

时间:2018-04-10 17:35:01

标签: django django-urls django-middleware

我按照Django教程编写了一个中间件类,准确地说是一个" Login-Required" -Middleware,这样用户只有在登录时才能在网站上做一些事情。 / p>

问题是中间件应该工作,但我不能再访问Django-Admin网站了。甚至在将admin-url添加到例外列表之后。他一直将我重定向到正常的登录网站"。

这是我的中间件

import re

from django.conf import settings
from django.shortcuts import redirect
from django.contrib.auth import logout
from django.urls import reverse


EXEMPT_URLS = [re.compile(settings.LOGIN_URL.lstrip('/'))]
if hasattr(settings, 'LOGIN_EXEMPT_URLS'):
    EXEMPT_URLS += [re.compile(url) for url in settings.LOGIN_EXEMPT_URLS]

class LoginRequiredMiddleware(object):
    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('/')
        print (path)

        # if not request.user.is_authenticated():
        #     if not any(url.match(path) for url in EXEMPT_URLS):
        #         return redirect(settings.LOGIN_URL)

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

        if path == reverse('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)

这是settings.py中的Login_EXEMPT_URLS

LOGIN_EXEMPT_URLS = (
r'^admin/',
r'^kalender/login/$',
r'^kalender/logout/$',
r'^kalender/reset-password/$',
r'^kalender/reset-password/done/$',
r'^kalender/reset-password/complete/$',
r'^kalender/reset-password/$',
r'^kalender/reset-password/confirm/',
r'^kalender/returnjson/'
)

LOGIN_URL = '/kalender/login/'
LOGIN_REDIRECT_URL = '/kalender/'
LOGOUT_REDIRECT_URL = '/kalender/'
ADMIN_REDIRECT_URL = '/admin/'

这是我的urls.py

urlpatterns = [
    url(r'^$', views.home, name='home'),
    url(r'^login/$', login, {'template_name': 'kalender/login.html'}, name='login'),
    #url(r'^logout/$', logout, {'template_name': 'kalender/login.html'}, name='logout'),
    url(r'^logout/$', logout, name='logout'),
    url(r'^delete/$', views.delete, name='delete'),
    url(r'^delete2/$', views.delete2, name='delete2'),
    url(r'^addTimestamp/$', views.addTimestamp, name='addTimestamp'),
    url(r'^delete_many/$', views.delete_many, name='delete_many'),
    url(r'^add_many/$', views.add_many, name='add_many'),
    url(r'^addPT/$', views.addPT, name='addPT'),
    url(r'^deletePT/$', views.deletePT, name='deletePT'),
    url(r'^settings/$', views.edit_settings, name='edit_settings'),
    url(r'^nextMonth/$', views.nextMonth, name='nextMonth'),
    url(r'^previousMonth/$', views.previousMonth, name='previousMonth'),
    url(r'^view_profile/$', views.view_profile, name='view_profile'),
    url(r'^edit_profile/$', views.edit_profile, name='edit_profile'),
    url(r'^change_postbox/$', views.change_postbox, name='change_postbox'),
    url(r'^getNextTimestamp/$', views.getNextTimestamp, name='getNextTimestamp'),
    url(r'^returnjson/$', views.returnjson, name='returnjson'),
    url(r'^change-password$', views.change_password, name='change_password'),
    url(r'^reset-password/$', password_reset, {'template_name': 'kalender/reset_password.html'}, name='reset_password'),
    url(r'^reset-password/done/$', password_reset_done, {'template_name': 'kalender/reset_password_done.html'}, name='password_reset_done'),
    url(r'^reset-password/confirm/(?P<uidb64>[0-9A-Za-z]+)-(?P<token>.+)/$', password_reset_confirm, {'template_name': 'kalender/reset_password_formular.html'}, name='password_reset_confirm'),
    url(r'^reset-password/complete/$', password_reset_complete, {'template_name': 'kalender/reset_password_complete.html'}, name='password_reset_complete'),
    #url(r'sendEmail/^$', views.sendEmail, name='sendEmail'),
]
编辑:感谢我的帮助,我现在可以登录管理页面,但是一旦登录,我就会通过LOGIN_REDIRECT_URL重定向到我的家庭视图。但是我希望在登录后继续进入管理页面。我怎么做?你可以在这里看到重定向:( Pycharm中控制台的输出)

admin/
[21/May/2018 13:11:38] "GET /admin/ HTTP/1.1" 302 0
admin/login/
[21/May/2018 13:11:38] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1668
admin/login/
[21/May/2018 13:11:44] "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0
admin/
[21/May/2018 13:11:44] "GET /admin/ HTTP/1.1" 302 0
kalender/

1 个答案:

答案 0 :(得分:0)

密码重置确认网址类似于kalender/reset-password/confirm/abc-123/。你的正则表达没有一个匹配。您需要添加r'^kalender/reset-password/confirm/'

之类的内容