Search code examples
djangodjango-modelsdjango-formsdjango-viewsdjango-media

My form keep saying "This(image) field is required!" Django 3.0


I made a project with net-ninja on youtube now I am adding "uploading media" feature in my own project but it is not working although every thing is set just tell me where i m going wrong. My form is keep asking me to upload image file even though i had already done, every time i send a post request it renders the page again with no value in image field. what is wrong here, why it is not taking image input?Don't comment set the required to false that is not the solution i don't know why some people said this to others on stackoverflow when they asked the same question as me.

My model class looks like this
class Products(models.Model):
    name            = models.CharField(max_length=500, unique=True, )
    price           = models.IntegerField()
    stock           = models.IntegerField()
    date_added      = models.DateTimeField(verbose_name="date added", auto_now_add=True, )
    thumb           = models.ImageField(default='default.png', blank=True)
    profile         = models.ForeignKey(Profile, on_delete=models.CASCADE, default=None,)

    def __str__(self):
        return self.name

    class Meta():
        verbose_name_plural = "Products"


My form looks like this
class AddProduct(forms.ModelForm):
    name = forms.CharField(widget=forms.TextInput(attrs={'placeholder':'Product Name',  'required':'True', 'class': 'form-control'}))
    price = forms.IntegerField(widget=forms.NumberInput(attrs={'placeholder':'Set Price',  'required':'True', 'class': 'form-control'}))
    stock = forms.IntegerField(widget=forms.NumberInput(attrs={'placeholder':'Number of items',  'required':'True', 'class': 'form-control'}))
    thumb = forms.ImageField(required=False, widget=forms.ClearableFileInput(attrs={'placeholder':'Upload Picture', 'enctype' : 'multipart/form-data'}))

    class Meta():
        model = Products
        fields = ('name', 'price', 'stock','thumb', )


HTML looks like this
<form class="row contact_form" action="/shop/add_products" method="post">
                                {% csrf_token %}
                                {% for field in form %}
                                    <div class="col-md-12 form-group p_star">
                                        {{ field }}
                                        {{ field.errors }}
                                    </div>
                                {% endfor %}
                                <!-- {% for field in form %}
                                    {% for error in field.errors %}
                                        <div class="col-md-12 form-group p_star">
                                            {{ error }}
                                        </div>
                                    {% endfor %}
                                {% endfor %} -->
                                {% if form.non_field_errors %}
                                    <div class="col-md-12 form-group p_star">
                                        {{ form.non_field_errors }}
                                    </div>
                                {% endif %}
                                <div class="col-md-12 form-group">
                                    <button type="submit" value="submit" class="btn_3">
                                        Add Product
                                    </button>
                                </div>
                            </form>


My views file looks like this

@login_required(login_url='/shop/login')
def shop_add_products(request):
    if request.method == 'POST':
        form = AddProduct(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.profile = request.user
            instance.save()
            return redirect("/shop/products")
    else:
        form = AddProduct()

    return render(request, "pillow_site_html/add_product.html", { 'form':form })

Solution

  • Oh sorry i didn't understood the question here, you are getting that because in fact you are not sending the picture inside your form as you have to add this to your form so it can accept files handling

    <form class="row contact_form" action="/shop/add_products" method="post" enctype="multipart/form-data">