I use modelformset_factory
to upload image, but I want to separate the already saved image (A
in the below link) and upload image (B
in the below link) on two different html template.
How do I resolve this problem?
Here is my code below:
views.py
def post_image(request):
PictureFormSet = modelformset_factory(Picture, form=PictureForm, extra=3)
if request.method == 'POST':
formset = PictureFormSet(request.POST, request.FILES)
if formset.is_valid():
formset.save()
return HttpResponse("Upload done!!")
else:
return HttpResponse("Upload Failed!!")
else:
formset = PictureFormSet()
return render(request, "Image.html", {"formset": formset})
models.py
class Picture(models.Model):
article = models.ForeignKey("Article", related_name="article_photo", on_delete=models.CASCADE)
photo = models.ImageField(upload_to="photo", height_field=None, width_field=None, max_length=100)
first_photo = models.BooleanField(default=False)
urls.py
path('post/image/', post_image),
html
<html lang="zh-Hant-TW">
<head>
<meta charset="UTF-8">
<title>post_image</title>
</head>
<form action="" method="POST" enctype="multipart/form-data">{% csrf_token %}
{{ formset.management_form }}
<table>
{% for form in formset %}
{{ form }}
{% endfor %}
</table>
<input type="submit" value="Sumbit">
</form>