我主要使用 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)),
]
```