html - 有没有办法使用 django 将第一张图片和第二张图片的描述分开?
问题描述
所以对于我的网站来说,上传图片有两个部分,一个是为盒子上传图片,另一个是上传设备的图片,但是当它上传时,第一个图片描述和第二个图片描述是一起的。示例如下所示。
上传(不是我想要的) 我想要在我的数据库中的图片时会发生什么的图片。
视图.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)
模型.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">
...
重命名后,我们可以单独访问它们并放置适当的描述:
视图.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,
)
...
推荐阅读
- opencensus - 无法使用 opencensus 收集器将跟踪发布到后端
- python - 使用 django-nose 但不使用 Django 的 DiscoverRunner 运行测试时出现重复的关键错误
- ansible - Ansible 多主机传输
- python - 如何在 Python 中导入装饰器包装的函数?
- bash - 无法将环境变量从 makefile 目标传递到 bash 文件
- diff - 多个文件或数据结构上的 diff/delta 术语
- jsf - 如何使用 p:inputText 将转换器传递给复合组件?
- android - Am I supposed to edit the google-services.json file?
- python - 我如何在电报中为我的用户订阅私人频道
- r - 在 r 中使用 ggplot2 在 x 轴上的直方图中指定 bin