Django 速成笔记(5) 文件上传 多个文件上传 之三种方法
时间: 2024-05-01 11:19:50 浏览: 25
在 Django 中,有多种方式可以实现文件上传以及多个文件上传的功能。下面介绍三种常用的方式。
### 1. 使用 Django 自带的文件上传模块
Django 自带了文件上传模块 `django.core.files.uploadhandler`,可以使用它来处理文件上传。使用该模块的步骤如下:
1. 在 `settings.py` 中配置文件上传路径:
```python
MEDIA_ROOT = '/path/to/media/'
MEDIA_URL = '/media/'
```
2. 在需要上传文件的视图函数中,使用 `request.FILES` 获取上传的文件,然后保存文件:
```python
def upload_file(request):
if request.method == 'POST' and request.FILES:
file = request.FILES['file']
with open(os.path.join(settings.MEDIA_ROOT, file.name), 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
```
3. 在模板中添加文件上传表单:
```html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="file">
<button type="submit">上传</button>
</form>
```
### 2. 使用第三方插件 django-formtools
django-formtools 是一个 Django 第三方插件,提供了一些常用的表单处理工具,其中包括文件上传。使用该插件的步骤如下:
1. 安装 django-formtools:
```
pip install django-formtools
```
2. 在视图函数中使用 `FileUploadView` 处理文件上传:
```python
from formtools.wizard.views import SessionWizardView
from django.core.files.storage import FileSystemStorage
class FileUploadView(SessionWizardView):
file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT, 'uploads'))
def done(self, form_list, **kwargs):
file_data = [form.cleaned_data.get('file') for form in form_list]
for file in file_data:
with open(os.path.join(settings.MEDIA_ROOT, file.name), 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
return HttpResponse('文件上传成功')
```
3. 在模板中设置表单:
```html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ wizard.management_form }}
{% for form in wizard.forms %}
{{ form.as_p }}
{% endfor %}
<button type="submit">上传</button>
</form>
```
### 3. 使用第三方插件 django-storages
django-storages 是一个 Django 第三方插件,提供了多种文件存储后端支持,包括本地文件系统、Amazon S3、Google Cloud Storage 等。使用该插件的步骤如下:
1. 安装 django-storages:
```
pip install django-storages
```
2. 在 `settings.py` 中配置文件存储后端:
```python
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
AWS_ACCESS_KEY_ID = 'your-access-key-id'
AWS_SECRET_ACCESS_KEY = 'your-secret-access-key'
AWS_STORAGE_BUCKET_NAME = 'your-bucket-name'
AWS_S3_REGION_NAME = 'your-region-name'
```
3. 在视图函数中使用 `request.FILES` 获取上传的文件,然后保存文件:
```python
from django.core.files.storage import default_storage
def upload_file(request):
if request.method == 'POST' and request.FILES:
file = request.FILES['file']
default_storage.save(file.name, file)
```
4. 在模板中添加文件上传表单:
```html
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
<input type="file" name="file">
<button type="submit">上传</button>
</form>
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)