[Django]-Django: how to use upload_to property of an ImageField

5👍

The forms framework should take care of this for you. No need to save the files by hand unless you want to store them in some container other than your filesystem.

class UploadImageForm(forms.ModelForm):
    class Meta:
        model = GallryImage
...
# Sample view
def upload_file(request):
    if request.method == 'POST':
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/success/url/')
    else:
        form = UploadImageForm()
    return render_to_response('upload.html', {'form': form})

Leave a comment