Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set django upload_handler in admin?

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?

like image 467
swoei Avatar asked Feb 17 '10 15:02

swoei


1 Answers

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.

like image 87
Anentropic Avatar answered Sep 18 '22 13:09

Anentropic