【发布时间】:2021-08-07 20:32:41
【问题描述】:
我是 Django 和 Wagtail 的新手,并且一直在寻找一种方法来使用 Wagtail 在博客条目页面上实现“简单”的喜欢/不喜欢按钮。
我在我的页面模型中包含了一个 total_likes IntegerField,并希望在用户单击 html 模板上的按钮时在数据库中增加或减少这个 int。
用户不应该登录。我发现的大多数教程只针对注册用户处理这个问题,这不是我想要的。
如果有人能指出我正确的方向,我会很高兴。 models.py 代码如下。
我不明白如何从模板中调用函数。
class BlogEntry(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250, blank=False)
body = RichTextField(blank=True)
tags = ClusterTaggableManager(through=BlogEntryTag, blank=True)
categories = ParentalManyToManyField('blog.BlogCategory', blank=False)
total_likes = models.IntegerField(blank=False, default=0)
image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+"
)
streamBody = StreamField([
("text", blocks.StaticContentBlock()),
("quotes", blocks.QuoteBlock()),
("image_and_text", blocks.ImageTextBlock()),
("related_articles", blocks.RelatedArticlesBlock()),
], null=True, blank=True)
sidebarBody = StreamField([
("text", blocks.StaticContentBlock()),
("quotes", blocks.QuoteBlock()),
("image_and_text", blocks.ImageTextBlock()),
("related_articles", blocks.RelatedArticlesBlock()),
], null=True, blank=True)
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
MultiFieldPanel([
ImageChooserPanel("image"),
FieldPanel('date'),
FieldPanel('tags'),
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
], heading="Blog information"),
FieldPanel('intro'),
StreamFieldPanel("streamBody"),
]
sidebar_panels = [
MultiFieldPanel([
FieldPanel("sidebarBody"),
], heading="Sidebar Content")
]
edit_handler = TabbedInterface(
[
ObjectList(content_panels, heading="Custom"),
ObjectList(Page.promote_panels, heading="Promote"),
ObjectList(Page.settings_panels, heading="Settings"),
ObjectList(sidebar_panels, heading="Sidebar"),
]
)
def __str__(self):
return self.total_likes
def likePost(self):
self.total_likes += 1
def dislikePost(self):
self.total_likes -= 1
【问题讨论】: