Wagtail - 在不使用时将博客添加到主页 home_page.html
Wagtail - adding blogs to the homepage when not using home_page.html
我没有将 Wagtail 用于所有页面,而是将 Wagtail 添加到基于 Edge v2 的现有站点。我通过创建一个新闻应用程序(而不是博客应用程序)并将页面名称更改为 NewsIndexPage 等来自定义 wagtail 的安装。这意味着我在主应用程序文件夹中没有 models.py。出于某种原因,这也意味着新闻页面位于 pages/news 而不是 /news.
我想在此主页上显示帖子,但已将名称更改为来自博客的新闻,并且没有使用主页模型,我对如何实现这一点感到困惑。我在想我应该能够添加到 views.py 中的 "context" 来实现这一点,但无论我尝试了什么似乎都没有用。
我是新手,我确定我错过了一些东西,但我在学习这些东西时很开心,并且非常感谢任何指导来完成这项工作。
这是 news/models.py:
from django import forms
from django.db import models
# New imports added for ClusterTaggableManager, TaggedItemBase, MultiFieldPanel
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index
from wagtail.wagtailsnippets.models import register_snippet
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
@register_snippet
class NewsCategory(models.Model):
name = models.CharField(max_length=255)
icon = models.ForeignKey(
'wagtailimages.Image', null=True, blank=True,
on_delete=models.SET_NULL, related_name='+'
)
panels = [
FieldPanel('name'),
ImageChooserPanel('icon'),
]
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'news categories'
class NewsIndexPage(Page):
intro = RichTextField(blank=True)
def get_context(self, request):
context = super(NewsIndexPage, self).get_context(request)
# Get the full unpaginated listing of resource pages as a queryset -
# replace this with your own query as appropriate
blogpages = self.get_children().live().order_by('-first_published_at')
paginator = Paginator(blogpages, 6) # Show 6 resources per page
page = request.GET.get('page')
try:
blogpages = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
blogpages = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
blogpages = paginator.page(paginator.num_pages)
# make the variable 'resources' available on the template
context['blogpages'] = blogpages
return context
class NewsPageTag(TaggedItemBase):
content_object = ParentalKey('NewsPage', related_name='tagged_items')
class NewsTagIndexPage(Page):
def get_context(self, request):
# Filter by tag
tag = request.GET.get('tag')
blogpages = NewsPage.objects.filter(tags__name=tag)
# Update template context
context = super(NewsTagIndexPage, self).get_context(request)
context['blogpages'] = blogpages
return context
class NewsPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
tags = ClusterTaggableManager(through=NewsPageTag, blank=True)
categories = ParentalManyToManyField('news.NewsCategory', blank=True)
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('date'),
FieldPanel('tags'),
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
], heading="News information"),
FieldPanel('intro'),
FieldPanel('body'),
InlinePanel('gallery_images', label="Gallery images"),
]
class NewsPageGalleryImage(Orderable):
page = ParentalKey(NewsPage, related_name='gallery_images')
image = models.ForeignKey(
'wagtailimages.Image', on_delete=models.CASCADE, related_name='+'
)
caption = models.CharField(blank=True, max_length=250)
panels = [
ImageChooserPanel('image'),
FieldPanel('caption'),
]
在 mysite/views.py 中我定义了主页:
from django.views import generic
from contact.forms import ContactForm
class HomePage(generic.TemplateView):
template_name = "home.html"
def get_context_data(self, *args, **kwargs):
context=super(HomePage, self).get_context_data(*args, **kwargs)
context['form'] = ContactForm
return context
重新发布评论中的讨论,因为它解决了问题。
关于NewsIndexPage
的URL请看urls.py。我怀疑您使用 url(r'pages', include(wagtail_urls))
而不是 url(r'', include(wagtail_urls))
来从根目录提供 Wagtail 页面。但是请确保在所有香草 Django 路由之后添加它,否则它会拦截所有内容。
关于在主页上显示新闻文章,请尝试将 context['news'] = NewsPage.objects.live().order_by('-first_published_at')
添加到 get_context_data
。这将在模板中作为 news
.
提供
我没有将 Wagtail 用于所有页面,而是将 Wagtail 添加到基于 Edge v2 的现有站点。我通过创建一个新闻应用程序(而不是博客应用程序)并将页面名称更改为 NewsIndexPage 等来自定义 wagtail 的安装。这意味着我在主应用程序文件夹中没有 models.py。出于某种原因,这也意味着新闻页面位于 pages/news 而不是 /news.
我想在此主页上显示帖子,但已将名称更改为来自博客的新闻,并且没有使用主页模型,我对如何实现这一点感到困惑。我在想我应该能够添加到 views.py 中的 "context" 来实现这一点,但无论我尝试了什么似乎都没有用。
我是新手,我确定我错过了一些东西,但我在学习这些东西时很开心,并且非常感谢任何指导来完成这项工作。
这是 news/models.py:
from django import forms
from django.db import models
# New imports added for ClusterTaggableManager, TaggedItemBase, MultiFieldPanel
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index
from wagtail.wagtailsnippets.models import register_snippet
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
@register_snippet
class NewsCategory(models.Model):
name = models.CharField(max_length=255)
icon = models.ForeignKey(
'wagtailimages.Image', null=True, blank=True,
on_delete=models.SET_NULL, related_name='+'
)
panels = [
FieldPanel('name'),
ImageChooserPanel('icon'),
]
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'news categories'
class NewsIndexPage(Page):
intro = RichTextField(blank=True)
def get_context(self, request):
context = super(NewsIndexPage, self).get_context(request)
# Get the full unpaginated listing of resource pages as a queryset -
# replace this with your own query as appropriate
blogpages = self.get_children().live().order_by('-first_published_at')
paginator = Paginator(blogpages, 6) # Show 6 resources per page
page = request.GET.get('page')
try:
blogpages = paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
blogpages = paginator.page(1)
except EmptyPage:
# If page is out of range (e.g. 9999), deliver last page of results.
blogpages = paginator.page(paginator.num_pages)
# make the variable 'resources' available on the template
context['blogpages'] = blogpages
return context
class NewsPageTag(TaggedItemBase):
content_object = ParentalKey('NewsPage', related_name='tagged_items')
class NewsTagIndexPage(Page):
def get_context(self, request):
# Filter by tag
tag = request.GET.get('tag')
blogpages = NewsPage.objects.filter(tags__name=tag)
# Update template context
context = super(NewsTagIndexPage, self).get_context(request)
context['blogpages'] = blogpages
return context
class NewsPage(Page):
date = models.DateField("Post date")
intro = models.CharField(max_length=250)
body = RichTextField(blank=True)
tags = ClusterTaggableManager(through=NewsPageTag, blank=True)
categories = ParentalManyToManyField('news.NewsCategory', blank=True)
def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
search_fields = Page.search_fields + [
index.SearchField('intro'),
index.SearchField('body'),
]
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('date'),
FieldPanel('tags'),
FieldPanel('categories', widget=forms.CheckboxSelectMultiple),
], heading="News information"),
FieldPanel('intro'),
FieldPanel('body'),
InlinePanel('gallery_images', label="Gallery images"),
]
class NewsPageGalleryImage(Orderable):
page = ParentalKey(NewsPage, related_name='gallery_images')
image = models.ForeignKey(
'wagtailimages.Image', on_delete=models.CASCADE, related_name='+'
)
caption = models.CharField(blank=True, max_length=250)
panels = [
ImageChooserPanel('image'),
FieldPanel('caption'),
]
在 mysite/views.py 中我定义了主页:
from django.views import generic
from contact.forms import ContactForm
class HomePage(generic.TemplateView):
template_name = "home.html"
def get_context_data(self, *args, **kwargs):
context=super(HomePage, self).get_context_data(*args, **kwargs)
context['form'] = ContactForm
return context
重新发布评论中的讨论,因为它解决了问题。
关于NewsIndexPage
的URL请看urls.py。我怀疑您使用 url(r'pages', include(wagtail_urls))
而不是 url(r'', include(wagtail_urls))
来从根目录提供 Wagtail 页面。但是请确保在所有香草 Django 路由之后添加它,否则它会拦截所有内容。
关于在主页上显示新闻文章,请尝试将 context['news'] = NewsPage.objects.live().order_by('-first_published_at')
添加到 get_context_data
。这将在模板中作为 news
.