我正在尝试提交带有一些文本字段和图像字段的表单。并将其保存为新的模型对象。不能让它工作,所以我现在从头开始检查每个部分..
我如何处理(只是保存)视图中的图像字段?我需要写一个额外的处理程序吗?它不是模型形式,而是常规形式。PIL/Pillow 是从哪里来的。我应该导入它吗?我可以清理图像域吗?
我一直在尝试这个:
def UploadThis(request):
if request.method == 'POST':
form = TestForm(request.POST, request.FILES)
response = {}
if form.is_valid():
response["status"] = "OK"
justtesting = Newmodel()
cd = form.cleaned_data
justtesting.name = cd['name']
justtesting.type = cd['type']
justtesting.description = cd['description']
justtesting.picture = request.FILES['picture']
justtesting.save()
模型:
class Newmodel(models.Model):
name = models.CharField(max_length=50)
type = models.CharField(max_length=50)
description = models.CharField(max_length=140, blank=True)
picture = models.ImageField(upload_to='images/testfiles', blank=True)
def __unicode__(self):
return self.name
形式:
class TestForm(forms.Form):
name = forms.CharField(max_length=50)
type = forms.CharField(max_length=50)
description = forms.CharField(max_length=140)
picture = forms.ImageField()
def clean(self):
cleaned_data = self.cleaned_data
name = cleaned_data.get("name")
description = cleaned_data.get("description")
type = cleaned_data.get("type")
return cleaned_data