Im trying to submit a form with some textfields and an imagefield. And save it as a new model object. Cant get it to work so im going over each part from the start now..
How do i handle (just saving it) the imagefield in my views? Do i need to write an extra handler? It's not a modelform but a regular form. Where does PIL/PIllow come into this. Should I import it? Can i clean the imagefield?
I've been trying this:
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()
Model:
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
Form:
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