[Django]-How to get a image form django imageFile input

2👍

To get the images inputted in your html file,
use:

request.FILES["input name "]

1👍

To upload a file, you need to add enctype='multipart/form-data' to your HTML form. So your form should look like:

  <form method="POST" action="" enctype='multipart/form-data'>
    {% csrf_token %}
    <input type="submit"></input>
    {{ form }}
  </form>

and in your views, you need to access request.FILES to get the uploaded file. Your upload image view should be:

def uploadImageView(request):
    form = UploadImageForm(request.POST or None, request.FILES or None)
    if request.method == 'POST':
        if form.is_valid():
            form.save()
            # rest of the code.

Leave a comment