0

I am trying to upload files using Django's ImageField and forms everytime I click submit on the form. The form.is_valid returns false So I printed forms.errors It says

photo2
This field is required.

photo1
This field is required.

I select the image files I want to upload and it still says field is required.

Here is view of my settings.py

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'

Here is views.py

 def upload(request):
 if request.method=="POST":
     prod = Product()
     form = UploadForm(request.POST, request.FILES)
     if form.is_valid():
         prod.name = form.cleaned_data.get('name')
         prod.brand = form.cleaned_data.get('brand')
         prod.material = form.cleaned_data.get('material')
         prod.color = form.cleaned_data.get('color')
         prod.price = form.cleaned_data.get('price')
         prod.discount = form.cleaned_data.get('discount')
         prod.sex=form.cleaned_data.get('sex')
         prod.photo1 = form.cleaned_data('photo1')
         prod.photo2 = form.cleaned_data('photo2')
         prod.save()
         return render(request, 'upload.html', {'form': form})
     else:
         x = form.errors
         return render(request,'upload.html', {'alert':x}, {'form': form})

 else:
     form = UploadForm
     return render(request, 'upload.html', {'form': form})

Here is my models.py

class Product(models.Model):
    name = models.CharField(max_length=200, default='N/A')
    brand = models.CharField(max_length=50, default='N/A')
    material = models.CharField(max_length=50, default='N/A')
    color = models.CharField(max_length=20, default='N/A')
    price = models.IntegerField(default=0)
    discount = models.IntegerField(default=0)
    discountprice = models.IntegerField(default=0)
    photo1 = models.ImageField(upload_to='productphotos/')
    photo2 = models.ImageField(upload_to='productphotos/')

    Male = 'M'
    Female = 'F'
    Both = 'Both'

    Genders = ((Male, 'Male'),(Female, 'Female'), (Both, 'Both'))
    sex = models.CharField(choices=Genders, default=Male, max_length=6)

forms.py class UploadForm(forms.ModelForm):

    class Meta:
        model = Product
        fields = ['name', 'brand', 'material', 'sex', 'color', 'price', 'discount', 'photo1', 'photo2']

and in my template I am just using

<div>
 {{form}}
</div>

Thank you for help in advance. I can upload forms.py if needed.

3
  • 1
    where is your UploadForm code? Commented Jun 18, 2018 at 11:45
  • DId you add the enctype="multipart/form-data" attribute to the form tag in your template? Commented Jun 18, 2018 at 11:47
  • @schwobaseggl I uploaded my template code. Commented Jun 18, 2018 at 11:49

1 Answer 1

2

You need to provide the form markup in the template as it is not included when rendering {{ form }}:

<form action="." method="post" enctype="multipart/form-data">
    {% csrf_token %}  # if needed
    {{ form }}
</form>

The enctype="multipart/form-data" is essential when posting files from the form. See the docs on forms in general and those on file uploads in particular:

Note that request.FILES will only contain data if the request method was POST and the form that posted the request has the attribute enctype="multipart/form-data". Otherwise, request.FILES will be empty.

Sign up to request clarification or add additional context in comments.

2 Comments

I did it and now I get a new error saying 'dict' object not callable on line prod.photo1 = form.cleaned_data('photo1')
You are missing .get()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.