Тема: Відображає bound field на сайті, але неповинно(HTML, django, Python)
Отримує таке при відображені сторінки.
код відображення форми
<form method="post" action="." enctype="multipart/form-data" class="text-gray-300 placeholder:text-black">
{%csrf_token%}
<div class="space-y-4">
{{form.as_p}}
{{formset.management_form}}
{% for form in formset %}
{% if form.instance.image %}
<p>{{ form.instance.image.name }}</p>
{% endif %}
{{ form.non_field_errors }}
{{ form.hidden_fields }}
{% for field in form.visible_fields %}
{% if field.name != 'image' %}
{{ field.errors }}
{{ field.label_tag }} {{ field.field.widget }}
{% endif %}
{% endfor %}
{% endfor %}
<label for="id_images">Images:</label>
<input type="file" id="id_images" name="images" multiple>
</div>
{% if form.errors or form.non_field_errors or formset.non_form_errors %}
<div class="mb-3 p-6 bg-red-100 rounded-xl">
{% for field in form %}
{{ field.errors }}
{% endfor %}
{% for form in formset %}
{{ form.errors }}
{% for field in form %}
{{ field.errors }}
{% endfor %}
{% endfor %}
{{form.non_field_errors}}
{{formset.non_form_errors}}
</div>
{%endif%}
<button class="mt-2 py-2 px-8 text-lg rounded-full font-bold" style="color: #2C4341; background-color: #B3F963; font-family: anonymous pro;">Submit</button>
</form>
код з views.py
@login_required
def edit(request, pk):
item = get_object_or_404(Item, pk=pk, created_by=request.user)
ItemImageFormSet = modelformset_factory(ItemImage, form=NewItemImageForm, extra=0)
if request.method == "POST":
form = EditItemForm(request.POST, request.FILES, instance=item)
formset = ItemImageFormSet(request.POST, request.FILES, queryset=ItemImage.objects.filter(item=item))
if form.is_valid() and formset.is_valid():
item_form = form.save()
formset.save()
for image in request.FILES.getlist('images'):
ItemImage.objects.create(item=item_form, image=image)
return redirect("item:detail", pk=item.id)
else:
form = EditItemForm(instance=item)
formset = ItemImageFormSet(queryset=ItemImage.objects.filter(item=item))
return render(request, "item/form.html", {"form": form, "formset": formset, "title": "Edit Project"})
Якщо потрібно надати додаткову інформацію, то надам.