Helo, i got stuck here and cant figure out exactly where the problem is coming from. The form is not saving again after i edited the widgets field
this is the form
class ItemsForm(ModelForm):
title = forms.CharField(widget =forms.TextInput(attrs = {'class':'form-control',
'placeholder': 'Items name'}))
price = forms.CharField(widget =forms.TextInput(attrs = {'class':'form-control',
'placeholder': 'Item Price'}))
phone = forms.CharField(widget =forms.TextInput(attrs = {'class':'form-control',
'placeholder': 'Phone number'}))
location = forms.CharField(widget =forms.TextInput(attrs = {'class':'form-control',
'placeholder': 'Your location'}))
description = forms.CharField(widget =forms.Textarea(attrs = {'class':'form-control',
'placeholder': 'Enter description'}))
thumbnails = forms.ImageField(widget =forms.FileInput(attrs = {'class':'form-control',
}))
class Meta:
model =Items
exclude = ['user',]
# fields = ['title', 'price', 'phone', 'location', 'description', 'thumbnails']
class ImageForm(ModelForm):
image = forms.ImageField(widget =forms.FileInput(attrs = {'class':'form-control',
"multiple":True}))
# image = forms.FileField(
# label='image',
# widget=forms.ClearableFileInput(attrs={"multiple":True})
# )
class Meta:
model =Images
fields = ['image',]
this is the views
def itemupload(request):
items = Items.objects.all()
if request.method =='POST':
itemsforms =ItemsForm(request.POST, request.FILES, prefix ='items')
imageforms = ImageForm(request.POST, request.FILES, prefix = 'image')
file= request.FILES.getlist('image')
if all([itemsforms.is_valid(), imageforms.is_valid()]):
item = itemsforms.save(commit = False)
item.user = request.user
item.save()
# image =imageforms.save(commit=False)
for i in file:
Images.objects.create(item = item, image =i)
# image.user =request.user
# image.item = item
# image.save()
# for i in file:
# Images.objects.create(image=i)
return redirect('index')
return redirect('itemupload')
imageforms =ImageForm()
itemsforms = ItemsForm()
context = {
'imageforms': imageforms,
'itemsform':itemsforms,
}
return render(request, 'jijiapp/itemform.html', context)
this is the template
<form action="{% url 'itemupload' %}" method="post", enctype="multipart/form-data">
{% csrf_token %}
<div class = "row mb-3">
<label class="col-md-5 col-form-label">{{ itemsform.title.label }}</label>
<div class="col-sm-5 form-control ">
{{ itemsform.title }}
</div>
{{ itemsform.title.errors }}
</div>
<div class = "row mb-3">
<label class="col-md-2 col-form-label">{{ itemsform.price.label }}</label>
<div class="col-sm-10 form-control">
{{ itemsform.price }}
</div>
{{ itemsform.price.errors }}
</div>
<div class = "row mb-3">
<label class="col-md-2 col-form-label">{{ itemsform.phone.label }}</label>
<div class="col-sm-10 form-control">
{{ itemsform.phone }}
</div>
{{ itemsform.phone.errors }}
</div>
<div class = "row mb-3">
<label class="col-md-2 col-form-label">{{ itemsform.location.label }}</label>
<div class="col-sm-10 form-control">
{{ itemsform.location }}
</div>
{{ itemsform.location.errors }}
</div>
<div class = "row mb-3">
<label class="col-md-2 col-form-label">{{ itemsform.description.label }}</label>
<div class="col-sm-10 form-control">
<div class="col-sm-10 ">
{{ itemsform.description }}
</div>
{{ itemsform.description.errors }}
</div>
<div class = "row mb-3">
<label class="col-md-2 col-form-label">{{ itemsform.thumbnails.label }}</label>
<div class="col-sm-10 form-control">
{{ itemsform.thumbnails }}
</div>
{{ itemsform.thumbnails.errors }}
</div>
<div class = "row mb-3">
<label class="col-md-2 col-form-label">{{ imageforms.image.label }}</label>
<div class="col-sm-10 form-control">
{{ imageforms.image }}
</div>
{{ imageforms.image.errors }}
</div>
<button class="btn btn-secondary mb-5" >Save</button>
</form>
this is the model
class Items(models.Model):
user = models.ForeignKey(User, on_delete = models.CASCADE, related_name='useritem')
slug = models.SlugField(max_length =20, unique=True, default='slug')
title = models.CharField(max_length=10, null=True, blank =True)
description = models.CharField(max_length=1000, null=True, blank=True)
price = models.CharField(max_length=20, null=True, blank=True)
phone = models.CharField(max_length=20, null=True, blank=True)
location =models.CharField(max_length=20, null=True, blank=True)
post_date = models.DateTimeField(auto_now_add=True, null=True, blank=True)
thumbnails =models.ImageField(upload_to='thumbnails/', null=True, blank=True)
def __str__(self):
return self.title
class Meta:
ordering =['-post_date',]
verbose_name_plural ='Items'
class Images(models.Model):
# user = models.ForeignKey(User, on_delete = models.CASCADE, null=True, blank = True)
image =models.ImageField(upload_to='media/', null=True, blank=True)
item=models.ForeignKey(Items, on_delete = models.CASCADE, null=True, blank = True)
# items = models.ManyToManyField(Items)
def __str__(self):
return str(self.item.title)
class Meta:
verbose_name_plural ='Images'