Django——自定义前端上传文件
from django.db import models
class Mytb(models.Model):
file = models.FileField(upload_to='uploads/')
# models.FileField:文件类型的字段
# upload_to='uploads/':文件保存到uploads目录下(会自动创建uploads文件夹)
# 如果settings中配置了MEDIA_ROOT则会在MEDIA_ROOT下创建uploads,否在会在根目录下创建uploads
path('test/',test)
from django.shortcuts import render
from app01.models import Mytb
# Create your views here.
def test(request):
if request.method == 'POST':
file = request.FILES.get('file')
print(file)
Mytb.objects.create(file=file)
return render(request,'test.html')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
</body>
</html>
效果:



浙公网安备 33010602011771号