Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6812277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:25:47+00:00 2026-05-26T20:25:47+00:00

In order to provide progress feedback of file uploads I need to install a

  • 0

In order to provide progress feedback of file uploads I need to install a custom upload handler for a specific view. This is documented for “classical” Django views at

https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#modifying-upload-handlers-on-the-fly

For generic views, however, I could not find any instructions and I came up with the following:

from django.utils import importlib
from django.core.exceptions import ImproperlyConfigured
from django.views.decorators.csrf import csrf_protect

class UploadHandlerMixin(object):
    '''
        A mixin for Django generic views that installs a custom upload handler in front of
        the current chain of upload handlers.

        You specify the handler to install by overriding the 'upload_handler' attribute of
        the class, specifying the module and class name in the form 'path.to.module.class':

            class MyView(UploadHandlerMixin, View):
                upload_handler = 'path.to.module.MyUploadHandler'

        If you do not override 'upload_handler', no additional upload handler will be
        installed.

        If the CsrfViewMiddleware is installed (which is the default) then you must use
        your view as follows in your urls.py:

            from django.views.decorators.csrf import csrf_exempt
            url(r'^.../$', csrf_exempt(MyView.as_view()), ...),

        Internally, the UploadHandlerMixin mixin will install the upload handler and then
        perform the CSRF check. (This is necessary because the CSRF check inspects
        request.POST, and afterwards upload handlers cannot be changed, see documentation
        link given below.)

        The handler is installed as described in the Django documentation "Modifying upload handlers
        on the fly", see https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#modifying-upload-handlers-on-the-fly
    '''

    upload_handler = None

    def dispatch(self, request, *args, **kwargs):
        if not self.upload_handler is None:
            request.upload_handlers.insert(0, UploadHandlerMixin._instantiate_upload_handler(self.upload_handler, request))
        return _uploadhandler_dispatch(request, self, *args, **kwargs)

    @staticmethod
    def _instantiate_upload_handler(path, *args, **kwargs):
        i = path.rfind('.')
        module, attr = path[:i], path[i+1:]
        try:
            mod = importlib.import_module(module)
        except ImportError, e:
            raise ImproperlyConfigured('Error importing upload handler module %s: "%s"' % (module, e))
        except ValueError, e:
            raise ImproperlyConfigured('Error importing upload handler module. Is FILE_UPLOAD_HANDLERS a correctly defined list or tuple?')
        try:
            cls = getattr(mod, attr)
        except AttributeError:
            raise ImproperlyConfigured('Module "%s" does not define a "%s" upload handler backend' % (module, attr))
        return cls(*args, **kwargs)

@csrf_protect
def _uploadhandler_dispatch(request, view, *args, **kwargs):
    return super(UploadHandlerMixin, view).dispatch(request, *args, **kwargs)

Is this the “recommended way” to accomplish the task? Is it okay security-wise?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:25:48+00:00Added an answer on May 26, 2026 at 8:25 pm

    Answering myself, an alternative to a service-side solution (like Django with a custom upload handler) is a client-side solution like JQuery File Upload. It uses the fact that more recent browsers allow the upload progress to be read on the client. In my case, this was much simpler to implement and causes no additional server load.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, in order to provide full disclosure, I want to point out that this
In order to provide a service for webmasters, I need to download the public
In order to provide nice URLs between parts of our app we split everything
I have an on-screen keyboard in order to provide a safer input for passwords.
According to the example in the manual of 'Robot Framework', in order to provide
Does ruby or rails provide a method to order strings in a specified order?
Anyone have a good resource or provide a sample of a natural order sort
I have been provided with a C++ DLL and associated header file in order
I have a class that inherits from MemoryStream in order to provide some buffering.
I've an sqlite3 database for an iOs application, in order to provide an offline

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.