我在Django中开发一个应用程序,当我尝试上传JPEG图像时遇到了一个问题,问题是这样的:
Internal Server Error: /publish_offer/
Traceback (most recent call last):
File "C:\Users\G-FIVE\Desktop\Projects\revenue\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\G-FIVE\Desktop\Projects\revenue\venv\Lib\site-packages\django\utils\deprecation.py", line 136, in __call__
response = self.process_response(request, response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\G-FIVE\Desktop\Projects\revenue\venv\Lib\site-packages\django\middleware\clickjacking.py", line 27, in process_response
if response.get("X-Frame-Options") is not None:
^^^^^^^^^^^^
AttributeError: 'SafeString' object has no attribute 'get'
[19/Jun/2023 14:08:35] "POST /publish_offer/ HTTP/1.1" 500 66377
与PNG图像不会发生这种情况,我真的很困惑。我将分享我的代码:
我的模特:
class ImagenesOferta(models.Model):
imagen = models.ImageField(verbose_name='Imagen Oferta', blank=False, null=False, upload_to='img_ofertas/')
class Meta:
verbose_name = 'Imagen de Oferta'
verbose_name_plural = 'Imagenes de Ofertas'
ordering = ['-id']
class Oferta(models.Model):
titulo = models.CharField(verbose_name='Título', max_length=100, null=False, blank=False)
descripcion = models.TextField(verbose_name='Descripción', null=True, blank=True)
cantidad = models.PositiveSmallIntegerField(verbose_name='Cantidad', null=False, blank=False, default=1)
valor = models.DecimalField(verbose_name='Valor', max_digits=3, decimal_places=2, null=False, blank=False)
material = models.CharField(verbose_name='Material', max_length=20, null=False, blank=False, choices=MATERIAL_CHOICES)
imagenes = models.ManyToManyField(ImagenesOferta, verbose_name='Imagenes de la Oferta', blank=True)
usuario = models.ForeignKey(Usuario, verbose_name='Usuario', null=False, blank=False, on_delete=models.CASCADE)
is_active = models.BooleanField(verbose_name='Activo/Inactivo', default=True)
vendido = models.BooleanField(verbose_name='Vendido', default=False)
fecha_creacion = models.DateTimeField(verbose_name='Fecha de Creación', auto_now_add=True)
def __str__(self):
return self.titulo
class Meta:
verbose_name = 'Oferta'
verbose_name_plural = 'Ofertas',
ordering = ['-fecha_creacion']
我的观点:
@login_required(login_url='login')
def publish_offer(request):
form = formOferta()
if request.method == 'POST':
form = formOferta(request.POST, request.FILES)
if form.is_valid():
form.instance.usuario = request.user
oferta = form.save()
imgs = [
ImagenesOferta(imagen=image)
for image in request.FILES.getlist('imagenes')
]
ImagenesOferta.objects.bulk_create(imgs)
oferta.imagenes.add(*imgs)
return redirect('home')
else:
return form.errors
return render(request, 'publicar_oferta.html', {
'form': form
})
我在模板中上传多张图片的输入:
<div class="mb-3">
<h5 class="text-center my-4">Añade Imagenes</h5>
<label for="imagenes" class="form-label">Cargar Imagenes</label>
<input class="form-control-file" type="file" id="imagenes" name="imagenes" multiple>
</div>
如果有人能帮我解决这个问题,我真的很感激!!
1条答案
按热度按时间eimct9ow1#
您不能在视图中返回
form.errors
:form.errors
是SafeString
,而不是HpttResponse
。您可以将结果 Package 在响应中:但很有可能,您只是想让视图继续其流程,并重新渲染模板,表单显示错误:
请注意,在HTML
<form>
中,您应该指定enctype="multipart/form-data"
[mdn-docs]来编码文件,以便POST请求可以处理这些文件。