如何在admin中设置django upload_handler?

时间:2022-01-31 02:12:13

I'm trying to make a django upload progress bar within the django admin. The application is only a small part of the project, therefor I do not want to set the custom upload handler in the settings.py.

我正在尝试在django admin中创建一个django上传进度条。应用程序只是项目的一小部分,因此我不想在settings.py中设置自定义上传处理程序。

The upload_handler can be set with request.upload_handlers.insert(0, UploadProgressHandler(request)) but not within the add_view of the django admin class. The response is this exception:

upload_handler可以使用request.upload_handlers.insert(0,UploadProgressHandler(request))设置,但不能在django admin类的add_view中设置。响应是这个例外:

If you try to modify request.upload_handlers after reading from request.POST or request.FILES Django will throw an error.

如果您在从request.POST或request.FILES读取后尝试修改request.upload_handlers,Django将抛出错误。

I also tried doing this with a decorator over the add_view but then I do not know how to access the request.upload_handlers.

我也尝试在add_view上使用装饰器执行此操作,但后来我不知道如何访问request.upload_handlers。

Can someone help me out?

有人可以帮我吗?

1 个解决方案

#1


1  

Have a look at the source for the decorator that comes with the admin app:

看看管理员应用程序附带的装饰器的源代码:

def staff_member_required(view_func):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, displaying the login page if necessary.
    """
    @wraps(view_func)
    def _checklogin(request, *args, **kwargs):
        if request.user.is_active and request.user.is_staff:
            # The user is valid. Continue to the admin page.
            return view_func(request, *args, **kwargs)

The decorator 'wraps' the original view so you are able to check the request arg before calling the original view func with it.

装饰器“包装”原始视图,因此您可以在使用它调用原始视图func之前检查请求arg。

#1


1  

Have a look at the source for the decorator that comes with the admin app:

看看管理员应用程序附带的装饰器的源代码:

def staff_member_required(view_func):
    """
    Decorator for views that checks that the user is logged in and is a staff
    member, displaying the login page if necessary.
    """
    @wraps(view_func)
    def _checklogin(request, *args, **kwargs):
        if request.user.is_active and request.user.is_staff:
            # The user is valid. Continue to the admin page.
            return view_func(request, *args, **kwargs)

The decorator 'wraps' the original view so you are able to check the request arg before calling the original view func with it.

装饰器“包装”原始视图,因此您可以在使用它调用原始视图func之前检查请求arg。