27、Django-文件上传
上传规范:
1、文件上传必须为POST提交方式
2、表单'<form>'中文件上传时必须带有 enctype="multipart/form-data"时才会有包含文件内容的数据
3、表单中用<input type="file" name="xxx"> 标签上传文件
#在视图函数中
- 用request.FILES取文件框的内容
- file=request.FILES['xxx']
说明:
- FILES的key对应页面中file框的name值
- file绑定文件流对象
- file.name 可以取到文件名
- file.file 可以取到文件的字节流数据-具体的数据
配置文件的访问路径和存储路径:
- 在setting.py中设置MEDIA相关配置、Django把用户上传的文件统称为media资源
如:- MEDIA_URL = '/media/'
- MEDIA_ROOT = os.path.join(BASE_DIR, 'media') #要在项目下新建一个media目录
------------------------------------------------------------------------------------------
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
--------------------------------------------------------------------------------------
#MEDIA_URL和MEDIA_ROOT需要手动绑定路由
- 在主路由中添加:
from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
---------------------------------------------------------------------------------------
#如:
from django.contrib import admin
from django.urls import path
from download_csv import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('test_csv/', views.test_csv),
path('upload_file/', views.upload_file),
]
# 配置静态文件、媒体文件
#settings.MEDIA_URL:媒体文件访问路径
#settings.MEDIA_ROOT:媒体文件保存路径
#static():将媒体文件映射到访问路径
#当文件上传到服务器时,会自动将文件保存到MEDIA_ROOT指定的路径下
#访问图片:http://127.0.0.1:8000/media/picture/111.png
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
----------------------------------------------------------------------------------------------
#说明:等价于做了MEDIA_URL开头的路由,Django接到该特征请求后去MEDIA_ROOT路径查找资源
views.py
-------------------------------------------------------------------------------------------------
from upload_app import models
from django.views.decorators.csrf import csrf_exempt
@csrf_exempt
#文件上传
def upload_file(request):
if request.method == 'GET':
return render(request, 'upload_file.html')
elif request.method == 'POST':
title = request.POST.get('title')
file = request.FILES.get('file')
#将上传的图片文件名和路径保存到数据库中
models.Content.objects.create(title=title, picture=file)
return HttpResponse('文件上传成功')
---------------------------------------------------------------------------------------------------------
upload_file.html
-----------------------------------------------------------------------------------------------------
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
{# enctype="multipart/form-data" 表示上传文件、分段式提交 #}
{% csrf_token %}
<form action="/upload_file/" method="post" enctype="multipart/form-data">
<p>
{# 输入文件名 #}
<input type="text" name="title">
</p>
<p>
{# 拿文件名 #}
<input type="file" name="file">
</p>
<p>
{# 提交 #}
<input type="submit" value="上传">
</p>
</form>
</body>
</html>
-------------------------------------------------------------------------------------------
models.py
------------------------------------------------------------------------------
from django.db import models
class Content(models.Model):
title = models.CharField(verbose_name='标题', max_length=100)
# 图片路径
picture = models.FileField(upload_to='picture')
-----------------------------------------------------------------------------------