如何在 Django 中间件中忽略媒体和静态 URL

问题描述 投票:0回答:2

我正在尝试重定向无权查看该页面的用户。在我的数据库中,我保存了网址名称。示例 -

company_list

path('company/list', CompanyListView.as_view(), name='company_list'),

现在我正在使用 EXEMPT_URLS,其中保存了 url 名称,并且用户可以免除这些 url。

EXEMPT_URLS = [
    'login',
    'logout',
    'superadmin_dashboard',
    'admin_dashboard',
]

class PermissionRequiredMiddleware:
    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')

        current_url = resolve(request.path_info).url_name

        print(request.path_info)

        if request.user.is_authenticated and current_url not in EXEMPT_URLS :
           if request.user.userprofile.user_role_id == 1:
                return redirect('superadmin_dashboard')
            else:
                return redirect('admin_dashboard')

现在问题来了。就像我要添加一家公司及其徽标一样。当我要去

company_list
时,它向我显示列表,但不显示图像。

"GET /media/user_profiles/logo_L7T5FKg.png HTTP/1.1" 302

我发现了这个问题,当我评论

def process_view()
时,它在我的模板中显示图像。但是当我取消注释时它不会显示图像。基本上我的中间件阻止了
"GET /media/user_profiles/logo_L7T5FKg.png HTTP/1.1" 302

我如何将该图像网址命名并放入

EXEMPT_URLS

django django-models django-forms django-templates django-views
2个回答
0
投票

基本上,如果用户没有权限,我会重定向到仪表板。因此,在我的中间件中,

MEDIA and STATIC URL
得到了
HTTP response status code 302 Found
,因为对于
MEDIA AND STATIC URL
,我的自定义中间件也在运行。所以忽略我发现
request.path

from django.conf import settings
    ...

    MEDIA_URL = request.path.startswith(settings.MEDIA_URL)
    STATIC_URL = request.path.startswith(settings.STATIC_URL)

    if request.user.is_authenticated and current_url not in EXEMPT_URLS and not MEDIA_URL and not STATIC_URL:
        if request.user.userprofile.user_role_id == 1:
            return redirect('superadmin_dashboard')
        else:
            return redirect('admin_dashboard')

0
投票

您可以检查请求是否来自静态或媒体网址,如果是这样,则不执行任何操作

EXEMPT_URLS = [
    'login',
    'logout',
    'superadmin_dashboard',
    'admin_dashboard',
]

class PermissionRequiredMiddleware:
    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):
        # Skip middleware for media and static files requests
        if request.path.startswith(settings.MEDIA_URL) or request.path.startswith(settings.STATIC_URL):
            return None
        assert hasattr(request, 'user')

        current_url = resolve(request.path_info).url_name

        print(request.path_info)

        if request.user.is_authenticated and current_url not in EXEMPT_URLS :
            if request.user.userprofile.user_role_id == 1:
                return redirect('superadmin_dashboard')
            else:
                return redirect('admin_dashboard')
© www.soinside.com 2019 - 2024. All rights reserved.