如何在管理中设置django upload_handler?

发布于 2024-08-21 21:16:10 字数 460 浏览 3 评论 0原文

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

upload_handler 可以使用 request.upload_handlers.insert(0, UploadProgressHandler(request)) 设置,但不能在 django 管理类的 add_view 中设置。 响应是这样的异常:

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

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

有人可以帮我吗?

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.

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:

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

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.

Can someone help me out?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鹤舞 2024-08-28 21:16:10

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

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)

装饰器“包装”原始视图,以便您可以在调用原始视图函数之前检查请求参数。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文