0

I am using class based views for a form but image is not uploading in db, not showing any errors. here is my models.py

class ProductCreateModel(models.Model):
    user            = models.ForeignKey('accounts.SellerProfileModel',
                    related_name='product_seller',
                    on_delete=models.CASCADE,editable=False)
    title           = models.CharField(max_length=120)
    slug            = models/.SlugField(max_length=255,unique=True,blank=True)
    description     = models.TextField(max_length=250)
    orignal_price   = models.DecimalField(decimal_places=2, max_digits=8)
    discount        = models.DecimalField(decimal_places=2,max_digits=4)
    discount_price  = models.DecimalField(decimal_places=2,max_digits=8)
    image1           = models.ImageField(upload_to=upload_image_path,blank=True,null=True)

here is my forms.py

from django import forms
from .models import ProductCreateModel
class ProductCreateForm(forms.ModelForm):
    class Meta:
        model = ProductCreateModel
       fields = '__all__'

here is my views.py

class ProductCreateView(views.LoginRequiredMixin,ActiveSellerOnlyMixin,generic.CreateView,):
template_name = 'products/create_new_product.html'
model = ProductCreateModel
form_class = ProductCreateForm
success_url = reverse_lazy('home')
def form_valid(self, form):

    product = form.save(commit=False)
    user =  get_current_user(self.request)
    image1 = form.cleaned_data['image1']
    form.instance.user = request.user
    return super(ProductCreateView, self).form_valid(form)
    # product.save()

Now the form is submitted successfully but image is empty.

Pankaj Sharma
  • 2,185
  • 2
  • 24
  • 50
  • whats the image path, is it in the media folder? – Aleksei Maide Feb 07 '18 at 06:10
  • def get_filename_ext(filepath): base_name = os.path.basename(filepath) name, ext = os.path.splitext(base_name) return name, ext def upload_image_path(instance, filename): print('this is here') new_filename = random.randint(1,3910209312) name, ext = get_filename_ext(filename) final_filename = '{new_filename}{ext}'.format(new_filename=new_filename, ext=ext) print('path returning') return"product/media/images/{user}/{new_filename}/{final_filename}".format( new_filename=new_filename, final_filename=final_filename,user=instance.user) – Pankaj Sharma Feb 07 '18 at 06:12
  • if i need to edit the question? for defining the upload image path – Pankaj Sharma Feb 07 '18 at 06:13
  • Post your form template html file. – Astik Anand Feb 07 '18 at 06:31
  • {% extends 'products/base.html' %} {% block content %} {% load bootstrap3 %}

    Submit Form

    {% csrf_token %} {% bootstrap_form form %}
    {% endblock %}
    – Pankaj Sharma Feb 07 '18 at 06:34

1 Answers1

1
 <form class="firstform" method="POST"> {% csrf_token %} {% bootstrap_form form %} <input type="submit" class="btn btn default" value="Submit"> </form>

to

 <form class="firstform"  enctype="multipart/form-data" method="POST"> {% csrf_token %} {% bootstrap_form form %} <input type="submit" class="btn btn default" value="Submit"> </form>

add enctype="multipart/form-data" to form in html.

When you are writing client-side code, all you need to know is use multipart/form-data when your form includes any <input type="file"> elements.If you want know why see here.

Ykh
  • 7,567
  • 1
  • 22
  • 31