Uploading Images and files in django forms.
If you want to upload an image in a django model form, all you need is a ImageField in your django model. ImageField inherits all attributes and methods from FileField but also validates that the uploaded object is a valid image.
Next thing you need is to specify where the image is supposed to be uploaded. This is done via the upload_to option to tell django which subdirectory of MEDIA_ROOT should the image be uploaded to. Here I have defined a function issue_screenshot_path which returns the filepath of the image that is stored. Django AutoSlug is used to name the image, and it populates from another field called app. I used the slugfield so that names of the images that are uploaded have relationship to the field app. Also, the slugfield helps in preserving the uniqueness of the the image name.
The code for the model form is pretty much generic except for the fact that the template for the corresponding form, must have the attribute enctype = 'multipart/form-data'
The file data is received via requeset.FILES, a dictionary contatining a key for each FileField or ImageField in the form, by the view handling the form. It should be noted that request.FILES will only contain data if the request method was POST and the <form> that posted the request has the attribute enctype = 'multiplart/form-data'












