我没有在所有页面上使用Wagtail,而是将Wagtail添加到基于Edge v2的现有站点。我通过创建一个新闻应用程序(而不是博客应用程序)并将页面名称更改为NewsIndexPage等来定制wagtail的安装。这意味着我在主app文件夹中没有models.py。出于某种原因,这也意味着新闻页面是页面/新闻而不是/ news。
我想在这个主页上显示帖子但是已经将名称更改为来自博客的新闻,而不是使用HomePage模型我对如何实现这一点感到困惑。我认为我应该能够添加到views.py中的“上下文”来实现这一点,但无论我尝试过什么似乎都不起作用。
成为新人我确信我错过了一些东西,但我学到了很多乐趣,并且非常感谢任何指导这项工作。
这是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中我定义了HomePage:
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
答案 0 :(得分:1)
从评论中重新发布讨论,因为它解决了问题。
关于NewsIndexPage
的网址,请查看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
形式提供。