繁体   English   中英

使用Django Filer的Django 1.11图像库

[英]Django 1.11 Image Gallery with Django Filer

问题

我需要在产品页面上显示图像库。 当我们使用Django 1.6时,此方法有效,但此后已升级到Django 1.11(Big Process)。 我现在停留在如何使它在新环境中工作的问题上。 现在,单击Add Image会弹出一个弹出窗口,我可以在其中选择图像以及与之关联的区域(美国,加拿大,西班牙等),但是单击“保存”后,弹出标题将变为Popup closing...窗口Popup closing...并且永远不会关闭-也像未添加到库中。 我上传的图像本身已添加到文件管理器中的其余图像中,但未添加到ProductGallery模型中。

我得到了什么

的Django:1.11.7

Django-Filer:1.2.7

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

以防万一其他人遇到这个问题并想知道它是如何修复的。

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

具体来说,该项目覆盖了保存按钮功能的某些部分。 函数dismissAddRelatedObjectPopup以前被命名为dismissAddAnotherPopup

这些功能被覆盖,以提供上述ProductGallery概述的自定义功能。 Django调用了该函数,但这在与名为SelectBox东西相关的弹出窗口中引发了一个错误,该错误随后中断了正确保存模型所需的ajax调用。

希望这可以在将来对某人有所帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM