Есть ли способ разделить описание для первого и второго изображения с помощью django?
На моем сайте есть 2 части для загрузки изображений, одна - для загрузки изображений для коробки, а другая - для загрузки изображений оборудования, но когда они загружаются, описание первого изображения находится вместе с описанием второго изображения. Пример показан ниже.
Фотография того, что происходит при загрузке (не то, что я хотел)
картинка того, что я хотел видеть в своей базе данных.
views.py
@login_required()
def ReceptionUnserviceable(request):
descriptionbox = Photo.objects.all()
if request.method == 'POST':
data = request.POST
images = request.FILES.getlist('images')
for image in images:
photo = Photo.objects.create(
descriptionbox=data['descriptionbox'],
image=image,
serialno=data['serialno'],
partno=data['partno'],
reception=data['reception'],
customername=data['customername'],
descriptionequipment=data['descriptionequipment'],
)
return redirect('success')
context = {}
return render(request, 'ReceptionUnserviceable.html', context)
models.py
class Photo(models.Model):
class Meta:
verbose_name = 'Photo'
verbose_name_plural = 'Photos'
image = models.ImageField(null=False, blank=False)
descriptionbox = models.TextField()
serialno = models.TextField()
partno = models.TextField()
reception = models.TextField()
customername = models.TextField()
descriptionequipment = models.TextField()
def __str__(self):
return self.descriptionbox
Receptionunservicable.html
<form method='POST' action="" enctype="multipart/form-data">
{% csrf_token %}
<div>
<label>Description Of The Box</label>
<input required name="descriptionbox" type="text" placeholder="Enter a description" style="width:300px" class="form-control">
</div>
<br>
<div>
<label>Upload Box Photo</label>
<input required name="images" type="file" multiple class="form-control-file">
</div>
<br>
<div>
<label>Part Number</label>
<input required name="partno" type="text" placeholder="Enter part number" style="width:300px" class="form-control">
</div>
<br>
<div>
<label>Serial Number</label>
<input required name="serialno" type="text" placeholder="Enter serial number" style="width:300px" class="form-control">
</div>
<br>
<div>
<label>Reception</label>
<input name="reception" type="text" placeholder="Enter reception number" style="width:300px" class="form-control">
</div>
<br>
<div>
<label>Customer Name</label>
<input required name="customername" type="text" placeholder="Enter customer name" style="width:300px" class="form-control">
</div>
<div>
<label>Description Of The Equipment</label>
<input required name="descriptionequipment" type="text" placeholder="Enter a description" style="width:300px" class="form-control">
</div>
<br>
<div>
<label>Upload Equipment Photo</label>
<input required name="images" type="file" multiple class="form-control-file">
</div>
<br>
<button type='submit' style="width: 100px" class="btn btn-primary">Submit</button>
</form>
Как это выглядит на моем сайте
Один из способов - называть изображения по-разному, чтобы мы знали, какое из них относится к коробке, а какое - к оборудованию:
Receptionunservicable.html
...
<input required name="image_box" type="file" multiple class="form-control-file">
...
<input required name="image_equipment" type="file" multiple class="form-control-file">
...
После переименования мы можем получить к ним отдельный доступ и поместить соответствующее описание:
views.py
...
data = request.POST
file_descriptions = [ # This assumes that both images are tagged with <required>
{
"image": request.FILES["image_box"],
"descriptionbox": data['descriptionbox'],
"descriptionequipment": "", # Or better yet make the field <Photo.descriptionequipment> as <null=True> in the model
},
{
"image": request.FILES["image_equipment"],
"descriptionbox": "", # Or better yet make the field <Photo.descriptionbox> as <null=True> in the model
"descriptionequipment": data['descriptionequipment'],
},
]
for file in file_descriptions:
photo = Photo.objects.create(
serialno=data['serialno'],
partno=data['partno'],
reception=data['reception'],
customername=data['customername'],
**file,
)
...