如何在Wagtail中添加多图上传功能?

问题描述 投票:0回答:1

我主要使用 Django、Wagtail 和 API 序列化器,而不直接使用模板进行 HTML 渲染。我使用 Django 和 Wagtail 的 API 功能来渲染内容,而不是 HTML 模板。

目前,我正在寻求添加一个新功能:后端多图片上传。现在,使用 Wagtail,我一次只能上传一张图像。不过,我们需要修改这个功能,以便可以同时上传多张图片..

让我澄清一下我上传图片的观点。其中 wagtail/images/multiple/add 用户可以添加多个图像,但在默认情况下,我们一次只能添加一张图像。我需要的是在默认结构中实现上传多个图像的能力。

我的代码结构是这样的:

========================================

blocks.py:

from wagtail.images.blocks import ImageChooserBlock
from wagtail.models import Site, Page
from cms.core.models import get_rootless_url_path
import re
from wagtail.images.models import Image
from wagtail.templatetags.wagtailcore_tags import richtext


class ImageChooserRenditionsAPIBlock(ImageChooserBlock):
    def get_api_representation(self, value, context=None) -> dict:
        from .serializers import ImageSerializer

        return ImageSerializer(value).data


class GreeterBlock(StructBlock):
    label = TextBlock()
    # description = TextBlock()
    description =CustomRichTextBlock(features=['h1', 'h2', 'h3', 'h4', 'h5', 'bold', 'italic', 'ol', 'ul', 'hr', 'link', 'image', 'code', 'blockquote'] )  
    image = ImageChooserRenditionsAPIBlock(required=False)
    video = CustomDocumentChooserBl`ock(required=False)
    cards = StreamBlock(
        local_blocks=(("cards", CardBlock()),),
        required=False,
    )


```
serializer.py:

class ImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = get_image_model()
        # fields = ("id", "title", "width", "height", "renditions")
        fields = ("id", "title", "width", "height", "file")

```

urls.py:

from django.urls import path, include
from wagtail import urls as wagtail_urls
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.documents import urls as wagtaildocs_urls
from wagtail.api.v2.router import WagtailAPIRouter
from wagtail.documents.api.v2.views import DocumentsAPIViewSet
from wagtail.images.api.v2.views import ImagesAPIViewSet
from cms.meta import urls as meta_urls
from .views import CustomPagesAPIViewSet

api_router = WagtailAPIRouter("wagtailapi")
api_router.register_endpoint("pages", CustomPagesAPIViewSet)
api_router.register_endpoint("images", ImagesAPIViewSet)
api_router.register_endpoint("documents", DocumentsAPIViewSet)


urlpatterns = [
    path("", include(wagtailadmin_urls)),
    path("content/documents/", include(wagtaildocs_urls)),
    
]

api_urlpatterns = [
    path("", api_router.urls),
    path("meta/", include(meta_urls.urlpatterns)),
]

```

python django wagtail
1个回答
0
投票

抱歉,Wagtail CMS 已经具有一次上传多张图片的功能。只需使用图像界面即可。单击“添加图像”,然后您可以使用 按钮 选择多个图像(按住 [Shift] 或 [Ctrl] 键 在小部件中选择多个文件) 或直接拖放 将您的文件放到标记区域。请参阅随附的屏幕截图。我想可能不需要您的自定义上传功能。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.