This might be a stupid question but I’m stuck.
I read the documentation on file/image uploads in Django here but I’m not sure how to proceed: http://docs.djangoproject.com/en/1.2/topics/http/file-uploads/
I have a model defined as below:
class PicUploads(models.Model):
blog_post = models.ForeignKey(Post)
pic = models.ImageField(upload_to='pics/%Y/%m/%d')
class PicUploadsForm(forms.ModelForm):
class Meta:
model=PicUploads
exclude = ('blog_post',)
In my views file, how do I go about saving the image?
If I use the handle_uploaded_file function as defined in the documentation, the image gets stored in the defined directory but what do I do to save its path along with the foreign key in the PicUploads model? Why can’t I just use the save() method like I do with text data?
save()is all you need to call on aModelFormwith aFile/ImageField.The document you are looking at explains how to use some upload handler to customize the default behavior, it’s not required.
Just instantiate your modelform with
request.POSTandrequest.FILES, callsave()and all is done!