功能 - 接受图片
models.py
class photo(models.Model): class Meta: db_table = 'photo' verbose_name = '图片表' id = models.AutoField(primary_key=True) image = models.ImageField(upload_to='photos', default='avatar.jpg')# manage.py 同级自动增加photos文件夹放置图片
url.py
from django.urls import pathurlpatterns = [ # 上传图片 path("upload_photo/", upload_photo_fun),]
url.py
def upload_photo_fun(request): # 获取上传文件的处理对象 try: pic = request.FILES.get('pic') print(pic) # 输出图片名字 type = request.POST.get("type", None) data = {"type":type, "image":pic} photo.objects.create(**data) obj = photo.objects.get(type=type) name = obj.image return JsonResponse({"code": 200, 'message': '新增成功',"name":str(name)}) except Exception as e: return JsonResponse({"code": 500, 'error': str(e)})
| xx |
image |
| xx |
photos/xx.jpg |
| xx |
photos/cc.png |
调整nginx配置
http{ client_max_body_size 300m # 设定通过nginx上传文件的大小 # 表示客户端请求的最大可接受body大小 # 它出现在请求头部的Content-Length字段 # 如果请求大于指定的值,客户端将收到一个"Request Entity Too Large" (413)错误,通常在上传文件到服务器时会受到限制}