【问题标题】:Django 1.11 Image Gallery with Django Filer带有 Django Filer 的 Django 1.11 图像库
【发布时间】:2017-11-14 21:22:37
【问题描述】:

问题

我需要在产品页面上显示图片库。这在我们使用 Django 1.6 时有效,但后来升级到 Django 1.11(大进程)。我现在被困在如何让它在新环境中工作。现在单击Add Image 会弹出一个弹出窗口,我可以在其中选择图像以及与之相关的区域(美国、加拿大、西班牙等),但在单击“保存”后弹出标题更改为Popup closing...并且永远不会关闭 - 图像也不会添加到图库中。我自己上传的图片会添加到文件管理器中的其余图片中,但它不会添加到 ProductGallery 模型中。

我有什么

Django: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 模型中。如果需要,我很乐意提供更多背景信息,非常感谢任何帮助。

【问题讨论】:

    标签: python django image django-filer


    【解决方案1】:

    以防万一其他人遇到此问题并想知道它是如何解决的。

    事实证明,某些 django-admin 模板功能已被覆盖并导致了一些问题。

    具体来说,这个项目覆盖了部分保存按钮功能。函数dismissAddRelatedObjectPopup曾经被命名为dismissAddAnotherPopup

    这些功能已被覆盖,以便通过 ProductGallery 提供上述自定义功能。 Django 去调用该函数,但这在弹出窗口上抛出了一个错误,该错误与名为 SelectBox 的东西有关,然后破坏了正确保存模型所需的 ajax 调用。

    希望这可以帮助将来的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-05
      • 2017-12-19
      • 2013-07-05
      • 2018-01-02
      • 2017-10-18
      • 2018-01-09
      • 2014-07-13
      相关资源
      最近更新 更多