-1

I have being having issues with uploading an image file into my database. Here is the code.

models.py

    from django.db import models
    from datetime import datetime


    class Product(models.Model):

        name = models.CharField(max_length=1000, blank=False, null=False)
        image = models.FileField()
        price = models.IntegerField(default=0, blank=False, null=False)
        stock = models.IntegerField(default=0, blank=False, null=False)

forms.py

    from django import forms
    from .models import Product


    class ProductCreationForm(forms.ModelForm):
        class Meta:
            model = Product
            fields = (
                "name",
                "image",
                "price",
                "stock",
            )
    

views.py

    from .models import Product
    from .forms import ProductCreationForm


    def product_create(request):
        form = ProductCreationForm()

        if request.method == "POST":
            form = ProductCreationForm(request.POST)

            if form.is_valid():
                name = form.cleaned_data['name']
                image = form.cleaned_data['image']
                price = form.cleaned_data['price']
                stock = form.cleaned_data['stock']


                item = Product.objects.create(name=name, image=image, price=price, 
                                                                    stock=stock)
                item.save()

                return redirect('product-view')

            else:
                messages.info(request, 'Invalid information entry!')
                return redirect('product-create')

        else:
            return render(request, "create.html", {"form": form})

create.html

    {% if user.is_authenticated %}

            <form method="post">
                {% csrf_token %}

                {% for message in messages %}
                    {{ message }}
                {% endfor %}

                {{ form.as_p }}

                <input type="submit", value="Done">
            </form>

    {% endif %}

settings.py

    STATIC_URL = 'static/'
    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')


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

urls.py

    from django.conf.urls.static import static
    from django.conf import settings

    if settings.DEBUG:
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
        urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Everytime i create a new product from my html page, only the name, price and stock that gets saved. The image is left blank. is there some widget am to add in the forms.py or something else is missing

1

1 Answer 1

2

Pro first You are using the ModelForm and You don't have to write this part

            if form.is_valid():
                name = form.cleaned_data['name']
                image = form.cleaned_data['image']
                price = form.cleaned_data['price']
                stock = form.cleaned_data['stock']

                item = Product.objects.create(name=name, image=image, price=price, 
                                                                    stock=stock)
                item.save()

after is_valid() only add the file to the form You can access it from request.FILES then save it it will be like

form = ProductCreationForm(data=request.POST, files=request.FILES)
if form.is_valid():
   form.save()
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.