admin管理员组

文章数量:1125629

I have used a person as an author on the blog page, but when I open the blog edit page, I can only see the text which first name and last name, not the person's image. How can I display the image as well?

here in above image you can see i got blank image

This is my person model

class Person(
    WorkflowMixin,
    DraftStateMixin,
    LockableMixin,
    RevisionMixin,
    PreviewableMixin,
    index.Indexed,
    ClusterableModel,
):

    first_name = models.CharField("First name", max_length=254)
    last_name = models.CharField("Last name", max_length=254)

    image = models.ForeignKey(
        "wagtailimages.Image",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
    )

    panels = [
        MultiFieldPanel(
            [
                FieldRowPanel(
                    [
                        FieldPanel("first_name"),
                        FieldPanel("last_name"),
                    ]
                )
            ],
            "Name",
        ),
        FieldPanel("image"),
        PublishingPanel(),
    ]

 

This is relationship model

class BlogPersonRelationship(Orderable, models.Model):
    page = ParentalKey(
        "BlogPage", related_name="blog_person_relationship", on_delete=models.CASCADE
    )
    person = models.ForeignKey(
        "base.Person", related_name="person_blog_relationship", on_delete=models.CASCADE
    )
    
    panels = [
        FieldPanel("person"),
    ]

This is blog page model

class BlogPage(Page):

    introduction = models.TextField(help_text="Text to describe the page", blank=True)
    image = models.ForeignKey(
        "wagtailimages.Image",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
        help_text="Landscape mode only; horizontal width between 1000px and 3000px.",
    )
    body = StreamField(
        BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
    )

    content_panels = Page.content_panels + [
        MultipleChooserPanel(
            "blog_category_relationship",
            chooser_field_name="category",
            heading="Categories",
            label="Category",
            panels=None,
            min_num=1,
        ),
        FieldPanel("introduction"),
        FieldPanel("image"),
        FieldPanel("body"),
        MultipleChooserPanel(
            "blog_person_relationship",
            chooser_field_name="person",
            heading="Authors",
            label="Author",
            panels=None,
            min_num=1,
        )
    ]

here is the model code you can check now

I have used a person as an author on the blog page, but when I open the blog edit page, I can only see the text which first name and last name, not the person's image. How can I display the image as well?

here in above image you can see i got blank image

This is my person model

class Person(
    WorkflowMixin,
    DraftStateMixin,
    LockableMixin,
    RevisionMixin,
    PreviewableMixin,
    index.Indexed,
    ClusterableModel,
):

    first_name = models.CharField("First name", max_length=254)
    last_name = models.CharField("Last name", max_length=254)

    image = models.ForeignKey(
        "wagtailimages.Image",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
    )

    panels = [
        MultiFieldPanel(
            [
                FieldRowPanel(
                    [
                        FieldPanel("first_name"),
                        FieldPanel("last_name"),
                    ]
                )
            ],
            "Name",
        ),
        FieldPanel("image"),
        PublishingPanel(),
    ]

 

This is relationship model

class BlogPersonRelationship(Orderable, models.Model):
    page = ParentalKey(
        "BlogPage", related_name="blog_person_relationship", on_delete=models.CASCADE
    )
    person = models.ForeignKey(
        "base.Person", related_name="person_blog_relationship", on_delete=models.CASCADE
    )
    
    panels = [
        FieldPanel("person"),
    ]

This is blog page model

class BlogPage(Page):

    introduction = models.TextField(help_text="Text to describe the page", blank=True)
    image = models.ForeignKey(
        "wagtailimages.Image",
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name="+",
        help_text="Landscape mode only; horizontal width between 1000px and 3000px.",
    )
    body = StreamField(
        BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
    )

    content_panels = Page.content_panels + [
        MultipleChooserPanel(
            "blog_category_relationship",
            chooser_field_name="category",
            heading="Categories",
            label="Category",
            panels=None,
            min_num=1,
        ),
        FieldPanel("introduction"),
        FieldPanel("image"),
        FieldPanel("body"),
        MultipleChooserPanel(
            "blog_person_relationship",
            chooser_field_name="person",
            heading="Authors",
            label="Author",
            panels=None,
            min_num=1,
        )
    ]

here is the model code you can check now

Share Improve this question edited yesterday Dhruvin Kalathiya asked Jan 9 at 6:20 Dhruvin KalathiyaDhruvin Kalathiya 112 bronze badges 1
  • Thank you for adding details to your post. That helps a lot. I updated my answer. – Joanna Gorska Commented 10 hours ago
Add a comment  | 

1 Answer 1

Reset to default 0

If the problem you are having that the author image is not displaying in wagtail admin page, while you are editing post: If the author is a foreign key to Post or a snippet, then it will not show the image inside the wagtail form. The Wagtail default behaviour only shows images in the form if they are directly related to this Page, not foreign key.

To customise the admin you can use this solution (making a custom panel, where you render the image)

The easier solution may be to enable preview on the side, you can see the All of the page with all images from foreign keys as part of the template. A mobile phone icon is in the top right-hand corner of the Wagtail admin form. See this wagtail video that might help you. To have the preview working correctly you need to have the root page enabled (there is a warning displayed Wagtail admin if you don't have it set up). You also need the template with the right name in the right path. How to know if you have the template in the right path? If the preview gives you a yellow page stating that the template is missing, just read the error, it will tell you the exact path and file name where Wagtail is looking for the template. Alternatively in your model that inherits from Page add template=path/path/filename.html

本文标签: