问题
我需要在产品页面上显示图片库。这在我们使用 Django 1.6 时有效,但后来升级到 Django 1.11(大进程)。我现在被困在如何让它在新环境中工作。现在单击Add Image
会弹出一个弹出窗口,我可以在其中选择图像以及与之关联的区域(美国、加拿大、西班牙等),但在单击“保存”后弹出标题更改为Popup closing...
并且永远不会关闭 - 也是图像未添加到图库。我自己上传的图像已添加到文件管理器中的其余图像中,但未添加到 ProductGallery 模型中。
我有什么
姜戈:1.11.7
Django-Filer:1.2.7
Django 套装:0.2.25
香草视图:1.0.4
我有产品模型,这些产品与 ProductGallery 模型有多对多的关系,如下所示:
class Product(models.Model):
gallery = models.ManyToManyField('products.ProductGallery')
ProductGallery 应该包含允许上传任何一个的图像和视频,并提供一个列表以在前端进行迭代以用于显示目的。
ProductGallery定义为:
class ProductGallery(models.Model):
limit = models.Q(app_label='media', model='image') | models.Q(app_label='media', model='video')
order = models.PositiveIntegerField(default=0)
content_type = models.ForeignKey(ContentType, limit_choices_to=limit)
object_id = models.PositiveIntegerField(db_index=True)
content_object = generic.GenericForeignKey('content_type', 'object_id')
class Meta:
ordering = ('order',)
def __str__(self):
return six.text_type(self.content_object)
其中media.image
定义为:(我暂时忽略视频)
class Image(CountryInfoModel, models.Model):
image = FilerImageField(null=True, blank=True)
def __str__(self):
return str(self.image.name or self.image.original_filename)
我有一个添加新媒体的视图,如下所示:
class AddMedia(LoginRequiredMixin, StaffuserRequiredMixin, JsonRequestResponseMixin, GenericView):
require_json = True
def post(self, request, *args, **kwargs):
object_id = self.request_json["objectId"]
object_var = self.request_json["objectVarName"]
content_type_id = self.request_json["contentType"]
order = self.request_json["order"]
media_id = self.request_json["mediaId"]
media_type = self.request_json["mediaType"]
content_type = _get_content_type_or_404(content_type_id)
content_object = _get_object_or_404(content_type, object_id)
model_var = getattr(content_object, object_var)
try:
if media_type.lower() == "image":
obj = Image.objects.get(pk=media_id)
elif media_type.lower() == "video":
obj = Video.objects.get(pk=media_id)
else:
raise Http404("Invalid mediaType parameter: {0}".format(media_type))
media_item = model_var.create(content_object=obj)
media_item.order = order
media_item.save()
except model_var.model.DoesNotExist:
pass
return self.render_json_response({'message': "Order successfully updated"})
我认为这就是所有的部分。我不知道为什么当我单击“保存”时,图像根本没有保存到 ProductGallery 模型中。如果需要,我很乐意提供更多背景信息,非常感谢任何帮助。