|
| 1 | +# Django Chunk File Upload |
| 2 | + |
| 3 | +Django Chunk File Upload is an alternative utility that helps you easily edit Django's chunked, drag and drop file uploads. |
| 4 | + |
| 5 | +<img src="https://i.ibb.co/9y2SgmS/f-P5-Or-Gkxk0-Ynj00ct-G.webp" alt="f-P5-Or-Gkxk0-Ynj00ct-G" border="0"> |
| 6 | + |
| 7 | +Features |
| 8 | +---------- |
| 9 | +- Multiple file uploads. |
| 10 | +- Drag and Drop UI. |
| 11 | +- MD5 checksum file. |
| 12 | +- Chunked uploads: optimizing large file transfers. |
| 13 | +- Prevent uploading existing files with MD5 checksum. |
| 14 | +- Easy to use any models. |
| 15 | + |
| 16 | + |
| 17 | +Quickstart |
| 18 | +---------- |
| 19 | + |
| 20 | +Install Django Chunk File Upload: |
| 21 | +```shell |
| 22 | +pip install git+https://github.com/thewebscraping/django-chunk-file-upload.git |
| 23 | +``` |
| 24 | + |
| 25 | + |
| 26 | +Add it to your `settings.py`: |
| 27 | + |
| 28 | +```python |
| 29 | +INSTALLED_APPS = [ |
| 30 | + 'django_chunk_file_upload', |
| 31 | +] |
| 32 | +``` |
| 33 | + |
| 34 | +Add it to your `urls.py`: |
| 35 | + |
| 36 | + |
| 37 | +```python |
| 38 | +from django.urls import path, include |
| 39 | + |
| 40 | +urlpatterns = [ |
| 41 | + path("file-manager/", include("django_chunk_file_upload.urls")), |
| 42 | +] |
| 43 | +``` |
| 44 | + |
| 45 | +Run Demo |
| 46 | + |
| 47 | +Demo URL: http://127.0.0.1:8000/file-manager/uploads/ |
| 48 | +```shell |
| 49 | +cd examples |
| 50 | +python manage.py migrate |
| 51 | +python manage.py runserver |
| 52 | +``` |
| 53 | + |
| 54 | +Change default config: `settings.py` |
| 55 | + |
| 56 | +```python |
| 57 | +DJANGO_CHUNK_FILE_UPLOAD = { |
| 58 | + "chunk_size": 1024 * 1024 * 2, # # custom chunk size upload (default: 2MB). |
| 59 | + "upload_to": "custom_folder/%Y/%m/%d", # custom upload folder. |
| 60 | + "is_metadata_storage": True, # save file metadata |
| 61 | + "js": ( |
| 62 | + "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js", |
| 63 | + "https://cdnjs.cloudflare.com/ajax/libs/spark-md5/3.0.2/spark-md5.min.js", |
| 64 | + "https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js", |
| 65 | + ), # use cdn. |
| 66 | + "css": ( |
| 67 | + "custom.css" |
| 68 | + ) # custom css path. |
| 69 | +} |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +Custom Your Models |
| 74 | +---------- |
| 75 | + |
| 76 | +models.py |
| 77 | + |
| 78 | +```python |
| 79 | +from django.db import models |
| 80 | +from django_chunk_file_upload.models import FileManager |
| 81 | + |
| 82 | + |
| 83 | +class Tag(models.Model): |
| 84 | + name = models.CharField(max_length=255) |
| 85 | + |
| 86 | + |
| 87 | +class YourModel(FileManager): |
| 88 | + tags = models.ManyToManyField(Tag) |
| 89 | + custom_field = models.CharField(max_length=255) |
| 90 | + |
| 91 | +``` |
| 92 | + |
| 93 | +forms.py |
| 94 | + |
| 95 | +```python |
| 96 | +from django_chunk_file_upload.forms import ChunkedUploadFileForm |
| 97 | +from .models import YourModel |
| 98 | + |
| 99 | + |
| 100 | +class YourForm(ChunkedUploadFileForm): |
| 101 | + class Meta: |
| 102 | + model = YourModel |
| 103 | + fields = "__all__" |
| 104 | +``` |
| 105 | + |
| 106 | +views.py |
| 107 | + |
| 108 | +```python |
| 109 | +from django_chunk_file_upload.views import ChunkedUploadView |
| 110 | +from .forms import YourForm |
| 111 | + |
| 112 | + |
| 113 | +class CustomChunkedUploadView(ChunkedUploadView): |
| 114 | + form_class = YourForm |
| 115 | + # chunk_size = 1024 * 1024 * 2 # custom chunk size upload (default: 2MB). |
| 116 | + # upload_to = "custom_folder/%Y/%m/%d" # custom upload folder. |
| 117 | + # template_name = "custom_template.html" # custom template |
| 118 | +``` |
| 119 | + |
| 120 | +custom_template.html |
| 121 | +```html |
| 122 | +<form action="." |
| 123 | + method="post" |
| 124 | + id="chunk-upload-form"> |
| 125 | + {{ form.media }} |
| 126 | + {{ form }} |
| 127 | +</form> |
| 128 | +``` |
| 129 | + |
| 130 | +urls.py |
| 131 | + |
| 132 | +```pyhon |
| 133 | +from django.urls import path |
| 134 | +
|
| 135 | +from .views import CustomChunkedUploadView |
| 136 | +
|
| 137 | +urlpatterns = [ |
| 138 | + path("uploads/", CustomChunkedUploadView.as_view(), name="custom-uploads"), |
| 139 | +] |
| 140 | +``` |
| 141 | + |
| 142 | +This package is under development, only supports create view. There are also no features related to image optimization. Use at your own risk. |
0 commit comments