Wagtail API v2.4-如何在API中显示/获取图像的根URL

时间:2019-02-28 19:22:35

标签: wagtail

我是wagtail的新手,我正在创建一个以react.js为前端,而wagtail为后端的网站。我主要使用Wagtail的API和axios渲染内容以做出反应。就是说,我在从wagtail的API获取完整的绝对URL来渲染图像时遇到问题。例如,调用URL时的get响应为“ /media/images/ayoba-desk.2e16d0ba.fill-1500x1500.png”。如何获取绝对网址(即127.0.0.1:8000/../../)?

Model.py

from django.db import models
from modelcluster.fields import ParentalKey

from wagtail.core.models import Page
from django.db.models import TextField
from rest_framework import serializers
from wagtail.core.fields import RichTextField
from wagtail.admin.edit_handlers import FieldPanel, MultiFieldPanel, InlinePanel
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.images.api.fields import ImageRenditionField

from wagtail.search import index
from wagtail.api import APIField


class ProjectsPage(Page):
# db fields
project_h_one = models.CharField(max_length=250, default="Project Name")
project_h_two = models.CharField(
    max_length=250, default="Project Description")
project_intro_p = models.TextField(blank=True)
project_h2_date = models.CharField(
    max_length=250, default="Project Launch Date")
project_p = models.TextField(blank=True)
project_h2_tech_stack = models.CharField(
    max_length=250, default="Tech Stack")
project_tech_stack_description = RichTextField(blank=True, features=["ul"])
project_image = models.ForeignKey(
    'wagtailimages.Image',
    null=True,
    blank=True,
    on_delete=models.SET_NULL,
    related_name='+'
)

# Search index configuration
search_fields = Page.search_fields + [
    index.SearchField('project_h_one'),
    index.FilterField('project_h_two'),
    index.FilterField('project_intro_p'),
    index.FilterField('project_h2_date'),
    index.FilterField('project_p'),
    index.FilterField('project_h2_tech_stack'),
    index.FilterField('project_tech_stack_description'),
]

# Editor panels configuration
content_panels = Page.content_panels + [
    FieldPanel('project_h_one'),
    FieldPanel('project_h_two', classname="full"),
    FieldPanel('project_intro_p', classname="full"),
    FieldPanel('project_h2_date', classname="full"),
    FieldPanel('project_p', classname="full"),
    FieldPanel('project_h2_tech_stack', classname="full"),
    FieldPanel('project_tech_stack_description', classname="full"),
    ImageChooserPanel('project_image'),
]
promote_panels = [
    MultiFieldPanel(Page.promote_panels, "Common page configuration"),
]

# API configuration
api_fields = [
    APIField('project_h_one'),
    APIField('project_h_two'),
    APIField('project_intro_p'),
    APIField('project_h2_date'),
    APIField('project_p'),
    APIField('project_h2_tech_stack'),
    APIField('project_tech_stack_description'),
    APIField('project_image'),
    APIField('project_img', serializer=ImageRenditionField(
        'fill-1500x1500', source='project_image')),

]

0 个答案:

没有答案