wagtail 页面在 `live` 状态下返回 `none` URL
我在使用wagtail页面时遇到了一些问题。
从命令行看
>>> Site.get_site_root_paths()
[(1, u'/home/', u'http://localhost')]
>>> BlogPage.objects.all()[0]
<BlogPage: Hello wagtail>
>>> BlogPage.objects.all()[0].url
>>> BlogPage.objects.all()[0].full_url
>>> BlogPage.objects.all()[0].status_string
'live'
>>> BlogPage.objects.all()[0].url_path
u'/blog/hello-wagtail/'
一开始一切都正常,我把Blog Page
从root
移动到了Blog Index Page
,在wagtail的管理界面中(下面有models.py
的代码)。但不知道为什么,我移动的页面在管理界面中消失了。于是我尝试通过重新创建数据库来重复之前的步骤,使用了./manage.py sycndb
和./manage.py migrate
,然后又创建了页面,但现在网址不再显示了。
我在wagtailcore/models.py
中设置了一个断点,想看看发生了什么。关键的部分似乎在这里:
@property
def url(self):
"""
Return the 'most appropriate' URL for referring to this page from the pages we serve,
within the Wagtail backend and actual website templates;
this is the local URL (starting with '/') if we're only running a single site
(i.e. we know that whatever the current page is being served from, this link will be on the
same domain), and the full URL (with domain) if not.
Return None if the page is not routable.
"""
root_paths = Site.get_site_root_paths()
for (id, root_path, root_url) in Site.get_site_root_paths():
if self.url_path.startswith(root_path):
return ('' if len(root_paths) == 1 else root_url) + self.url_path[len(root_path) - 1:]
self.url_path.startswith(root_path)
在我的情况下永远为假。
在那个循环中的变量:
id = {int} 1
root_path = {unicode} u'/home/'
root_paths = {list} [(1, u'/home/', u'http://localhost')]
root_url = {unicode} u'http://localhost'
self = {Page} Blog
这意味着我创建的页面无法被访问。虽然我可以通过wagtail管理界面的预览模式正确查看我的页面,但我找不到为什么我的页面没有网址可用的原因 :(。
这是我的models.py
from django.db import models
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailadmin.edit_handlers import FieldPanel, MultiFieldPanel, InlinePanel, PageChooserPanel
from modelcluster.fields import ParentalKey
class BlogPage(Page):
body = RichTextField()
intro = RichTextField()
date = models.DateField("Post date")
indexed_fields = ('body', )
search_name = "Blog Page"
BlogPage.content_panels = [
FieldPanel('title', classname="full title"),
FieldPanel('date'),
FieldPanel('intro', classname="full"),
FieldPanel('body', classname="full"),
]
class LinkFields(models.Model):
link_page = models.ForeignKey(
'wagtailcore.Page',
null=True,
blank=True,
related_name='+'
)
panels = [
PageChooserPanel('link_page'),
]
class Meta:
abstract = True
class RelatedLink(LinkFields):
title = models.CharField(max_length=255, help_text="Link title")
panels = [
FieldPanel('title'),
MultiFieldPanel(LinkFields.panels, "Link"),
]
class Meta:
abstract = True
class BlogIndexPageRelatedLink(Orderable, RelatedLink):
page = ParentalKey('main.BlogIndexPage', related_name='related_links')
class BlogIndexPage(Page):
intro = models.CharField(max_length=256)
indexed_fields = ('body', )
search_name = "Blog Index Page"
BlogIndexPage.content_panels = [
FieldPanel('title', classname="full title"),
FieldPanel('intro', classname="full"),
InlinePanel(BlogIndexPage, 'related_links', label="Related links"),
]
1 个回答
10
一般来说,你应该把页面放在主页的子页面下。这意味着你的博客首页的地址会是 /home/blog/
,而因为 /home/
在根路径列表中对应的是 http://localhost
,所以最终的地址就是 http://localhost/blog/
。
正如你所看到的,如果你在主页旁边直接创建页面,它们就会不在默认的网站记录里,这样就不会有可以访问的地址。不过,你可以通过Django的管理界面在 http://localhost/django-admin/
设置额外的网站。例如,如果你想把博客放在 http://blog.example.com/,你需要为这个域名创建一个网站条目,并把它指向你的博客首页。