106 lines
3.6 KiB
Python
106 lines
3.6 KiB
Python
import os
|
||
|
||
from django.shortcuts import render, redirect, HttpResponse
|
||
from django import forms
|
||
|
||
from app import models
|
||
from app.utils.bootstrap import BootstrapForm, BootstrapModelForm
|
||
from django.conf import settings
|
||
|
||
|
||
class UpForm(BootstrapForm):
|
||
"""文件上传演示表单 """
|
||
|
||
exclude_filed = ['img'] # 过滤字段不让引用Bootstrap样式
|
||
|
||
name = forms.CharField(label='姓名')
|
||
age = forms.IntegerField(label='年龄')
|
||
img = forms.FileField(label='头像')
|
||
|
||
|
||
def upload_list(request):
|
||
""" 上传文件 """
|
||
if request.method == 'GET':
|
||
return render(request, 'upload_list.html')
|
||
|
||
# POST请求
|
||
file_object = request.FILES.get('avator')
|
||
file_name = file_object.name
|
||
|
||
# 将接收到的文件,分块写入
|
||
with open(f'app/static/upload/{file_name}', 'wb') as f:
|
||
for chunk in file_object.chunks():
|
||
f.write(chunk)
|
||
|
||
return HttpResponse('上传成功')
|
||
|
||
|
||
def upload_form(request):
|
||
"""Form表单上传文件演示 """
|
||
title = 'Form上传'
|
||
|
||
# get请求,只显示空白表单
|
||
if request.method == 'GET':
|
||
form = UpForm()
|
||
return render(request, 'upload_form.html', {'form': form, 'title': title})
|
||
|
||
form = UpForm(data=request.POST, files=request.FILES)
|
||
if form.is_valid():
|
||
print(form.cleaned_data)
|
||
# cleaned_data = {'name': 'tom', 'age': 24, 'img': < InMemoryUploadedFile: 780.jpg(image / jpeg) >}
|
||
# 下一步就是处理获取到数据
|
||
|
||
# 对于上传的图像来说,先保存到本地,数据表中存储路径
|
||
image_object = form.cleaned_data.get('img')
|
||
file_name = image_object.name
|
||
|
||
# 在这里没有用windows的方式,
|
||
# 为了确保在不同系统间都可以用到,
|
||
# 所以用os拼接路径 (app/upload/xxx.jpg)
|
||
|
||
# 重点:django只认静态文件保存在static目录下,用户上传的文件在media目录下(需配置)
|
||
# 所以想展示图片,就保存在media目录下
|
||
# file_path = os.path.join(settings.MEDIA_ROOT, 'upload', file_name) # 取得是绝对路径D:\xx\xxx\xxx
|
||
file_path = os.path.join('media', 'upload', file_name)
|
||
|
||
# 将接收到的文件,分块写入
|
||
with open(file_path, 'wb') as f:
|
||
for chunk in image_object.chunks():
|
||
f.write(chunk)
|
||
|
||
# 保存数据到数据库 name,age,img对应表字段,值是从form.cleaned_data获取到的
|
||
# 因为form.cleaned_data里面最初保存的是一个文件对象,所以把他替换为文件路径
|
||
models.Boss.objects.create(
|
||
name=form.cleaned_data['name'],
|
||
age=form.cleaned_data['age'],
|
||
img=file_path, # 图片地址保存的是static目录下
|
||
)
|
||
|
||
return HttpResponse('上传成功')
|
||
|
||
return render(request, 'upload_form.html', {'form': form, 'title': title})
|
||
|
||
|
||
class UploadModelForm(BootstrapModelForm):
|
||
exclude_filed = ['img'] # 过滤字段不让引用Bootstrap样式
|
||
class Meta:
|
||
model = models.City
|
||
fields = '__all__'
|
||
|
||
|
||
|
||
def upload_modelform(request):
|
||
""" 上传文件 """
|
||
if request.method == 'GET':
|
||
form = UploadModelForm()
|
||
return render(request, 'upload_form.html', {'form': form,'title':'ModelForm上传文件'})
|
||
|
||
form = UploadModelForm(data=request.POST, files=request.FILES)
|
||
if form.is_valid():
|
||
# 对于文件会自动保存
|
||
# 路径是在models中定义的路径
|
||
# 字段中保存的是路径字符串
|
||
form.save()
|
||
return HttpResponse('上传成功')
|
||
|
||
return render(request, 'upload_form.html', {'form': form, 'title': 'ModelForm上传文件'}) |