Can I slow down Django

前端 未结 6 636
轻奢々
轻奢々 2021-02-05 10:12

Simple question really

./manage.py runserver

Can I slow down localhost:8000 on my development machine so I can simulate file uploa

6条回答
  •  旧巷少年郎
    2021-02-05 10:55

    Use the slow file upload handler from django-gubbins:

    import time
    from django.core.files.uploadhandler import FileUploadHandler
    
    class SlowFileUploadHandler(FileUploadHandler):
        """
        This is an implementation of the Django file upload handler which will
        sleep between processing chunks in order to simulate a slow upload. This
        is intended for development when creating features such as an AJAXy
        file upload progress bar, as uploading to a local process is often too
        quick.
        """
        def receive_data_chunk(self, raw_data, start):
            time.sleep(2)
            return raw_data
    
        def file_complete(self, file_size):
            return None
    

    You can either enable this globally, by adding it to:

    FILE_UPLOAD_HANDLERS = (
        "myapp.files.SlowFileUploadHandler",
        "django.core.files.uploadhandler.MemoryFileUploadHandler",
        "django.core.files.uploadhandler.TemporaryFileUploadHandler",
    )
    

    Or enable it for a specific request:

    request.upload_handlers.insert(0, SlowFileUploadHandler())
    

    Make sure the request is excepted from CSRF checking, as mentioned at https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#id1

提交回复
热议问题