我很难理解为什么我的 {{ post.categories }}
在模板中,它以 blog.PostPageBlogCategory.None
当“管理”面板显示所选类别时 post
对象
这是我的model.py设置:
from django.db import models
# Create your models here.
from django.db import models
from modelcluster.fields import ParentalKey
from modelcluster.tags import ClusterTaggableManager
from taggit.models import Tag as TaggitTag
from taggit.models import TaggedItemBase
from wagtail.admin.edit_handlers import (
FieldPanel,
FieldRowPanel,
InlinePanel,
MultiFieldPanel,
PageChooserPanel,
StreamFieldPanel,
)
from wagtail.core.models import Page
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.snippets.edit_handlers import SnippetChooserPanel
from wagtail.snippets.models import register_snippet
class BlogPage(Page):
description = models.CharField(max_length=255, blank=True,)
content_panels = Page.content_panels + \
[FieldPanel("description", classname="full")]
class PostPage(Page):
header_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
tags = ClusterTaggableManager(through="blog.PostPageTag", blank=True)
content_panels = Page.content_panels + [
ImageChooserPanel("header_image"),
InlinePanel("categories", label="category"),
FieldPanel("tags"),
]
class PostPageBlogCategory(models.Model):
page = ParentalKey(
"blog.PostPage", on_delete=models.CASCADE, related_name="categories"
)
blog_category = models.ForeignKey(
"blog.BlogCategory", on_delete=models.CASCADE, related_name="post_pages"
)
panels = [
SnippetChooserPanel("blog_category"),
]
class Meta:
unique_together = ("page", "blog_category")
@register_snippet
class BlogCategory(models.Model):
CATEGORY_CHOICES = (
('fighter', 'Fighter'),
('model', 'Model'),
('event', 'Event'),
('organization', 'Organization'),
('other', 'Other')
)
name = models.CharField(max_length=255)
slug = models.SlugField(unique=True, max_length=80)
category_type = models.CharField(
max_length=100, choices=CATEGORY_CHOICES, blank=True)
description = models.CharField(max_length=500, blank=True)
panels = [
FieldPanel("name"),
FieldPanel("slug"),
FieldPanel("category_type"),
FieldPanel("description"),
]
def __str__(self):
return self.name
class Meta:
verbose_name = "Category"
verbose_name_plural = "Categories"
class PostPageTag(TaggedItemBase):
content_object = ParentalKey("PostPage", related_name="post_tags")
@register_snippet
class Tag(TaggitTag):
class Meta:
proxy = True
我的blog_page.html模板上的一个小上下文。此模板是在博客中创建的所有帖子的列表,而不是单个帖子本身。
{% extends "base.html" %}
{% load wagtailcore_tags wagtailimages_tags %}
{% block content %}
<section class="text-gray-600 body-font">
<div class="container px-5 py-24 mx-auto">
<div class="flex flex-wrap -m-4">
{% for post in page.get_children.specific %}
<div class="p-4 md:w-1/3">
<div
class="
h-full
border-2 border-gray-200 border-opacity-60
rounded-lg
overflow-hidden
"
>
{% if post.header_image %} {% image post.header_image original as header_image %}
<a href="{% pageurl post %}">
<img
src="{{ header_image.url }}"
class="lg:h-48 md:h-36 w-full object-cover object-center"
/>
</a>
{% endif %}
<div class="p-6">
<h2
class="
tracking-widest
text-xs
title-font
font-medium
text-gray-400
mb-1
"
>
{{ post.categories}}
</h2>
## The rest omitted for brevity
现在,我可以从中提取数据了 post
对象,例如日期、标题、图像,但由于某种原因,我在models.py中设置类别的方式无法为每个帖子显示正确的类别。
以下是模板和管理员的图像,以了解更多上下文:
如你所见 blog.PostPageBlogCategory.None
正在展示自己 {{post.categories}}
. 理想情况下,显示的正确类别应该是string对象,而不是none。
我的模特做错了什么?
1条答案
按热度按时间mi7gmzs61#
这是django的怪癖。。。
post.categories
不会为您提供类别列表本身,而是提供一个管理器对象,该对象提供对该关系的各种操作(我不知道为什么管理器对象的字符串表示出现为"blog.PostPageBlogCategory.None"
,尽管…)post.categories.all
将为您提供PostPageBlogCategory
对象,但由于您没有提供__str__
方法,直接输出可能也不会显示任何有意义的内容。在它上面循环应该会给你你想要的: