I am so close to having this all figured out but the form always returns invalid because the image returns None. I imagine it’s something tiny considering as I was typing this I found “image= form.cleaned_date[‘images’]” in the forms.py lol, but that wasn’t the entire problem, it stills returns as None and I just don’t know why. I’ve been through this code with a fine tooth comb.
#views.py
formset_dictionary_copy = request.POST.copy()
formset = ArtFormSet(formset_dictionary_copy)
context = {
'formset': formset,
}
if formset.is_valid():
new_images = []
for form in formset:
title = form.cleaned_data.get('title')
images = form.cleaned_data.get('images')
short_description = form.cleaned_data.get('short_description')
description = form.cleaned_data.get('description')
for_sale = form.cleaned_data.get('for_sale')
created_on = form.cleaned_data.get('created_on')
if title and images and short_description and description and for_sale and created_on:
new_images.append(Art(title=title, images=images, short_description=short_description, description=description, for_sale=for_sale, created_on=created_on))
try:
with transaction.atomic():
Art.objects.bulk_create(new_images)
messages.success(request, 'Your upload was a success!')
context['formset'] = ArtFormSet()
return render(request, 'editors/gallery_editor.html', context)
except IntegrityError:
messages.error(request, 'There was an error uploading your images.')
return render(request, 'editors/gallery_editor.html', context)
context = {
'formset': formset,
}
return render(request, 'editors/gallery_editor.html', context)
else:
print(formset.errors)
formset_dictionary_copy = request.POST.copy()
formset = ArtFormSet(formset_dictionary_copy)
context = {
'formset': formset,
}
return render(request, 'editors/gallery_editor.html', context)
#forms.py
from gallery.models import Art
class ArtForm(forms.ModelForm):
class Meta:
model = Art
exclude = ()
class BaseArtFormSet(BaseFormSet):
def clean(self):
if any(self.errors):
return
titles = []
images = []
duplicates = False
for form in self.forms:
if form.cleaned_data:
title = form.cleaned_data['title']
image = form.cleaned_data['images']
if title and image:
if title in titles:
duplicates = True
titles.append(title)
if image in images:
duplicates = True
images.append(image)
if duplicates:
raise forms.ValidationError(
'You cannot have duplicate titles or images',
code = 'duplicates'
)
if image and not title:
raise forms.ValidationError(
'All images must have a title',
code='missing_title'
)
elif title and not image:
raise forms.ValidationError(
'You need an image to upload',
code='missing_image'
)
#gallery_editor.html
<main>
<form method="post" action="" enctype="multipart/form-data">
{% csrf_token %}
{{ formset.management_form}}
{% for form in formset %}
<div>{{ form.id }}{{ form }}</div>
{% endfor %}
<input type="hidden" value="false" name="additems" id="additems">
<button class="btn btn-primary" id="additemsbutton">Add items</button>
<input type="submit" value="Submit order" class="btn btn-primary">
</form>
</main>