我正在尝试将文件保存到磁盘,而不是在类或任何直接到磁盘的任何东西,但我不能为我的生活弄清楚如何。
到目前为止,我已经:
查看:`
def uploadview(request):
uploadtestvar='Uploadpage!'
request.session['test']='Is this working?'
if request.method == 'POST':
form=UploadForm(request.POST, request.FILES)
if form.is_valid():
forcetitle='test'
try:
pass
except:
pass
return HttpResponseRedirect('/test/')
else:
form=UploadForm()
return render(request, 'test/uploads.html',{'uploadtestvar':uploadtestvar, 'form':form})`
表格直接从Django被盗:
from django import forms
class UploadForm(forms.Form):
title=forms.CharField(max_length=50)
file=forms.FileField()
我搜索了无数的线程,发现没有一个给出一个简单的例子让我理解如何获取该请求.FILES ['file']保存到磁盘的某个地方。可能的文件类型是png,jpg和pdf。
答案 0 :(得分:0)
在FileSystemStorage具有存储目录的模型中将FileSystemStorage传递给FileField
from django.core.files.storage import FileSystemStorage
fs = FileSystemStorage(location = 'media/files/')
class UploadModel(models.Model):
title=forms.CharField(max_length = 50)
file=forms.FileField(storage = fs)