gpt4 book ai didi

python - 提供 `none` url 和 `live` 状态的 wagtail 页面

转载 作者:太空狗 更新时间:2023-10-29 20:56:10 25 4
gpt4 key购买 nike

我在使用 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来自 rootBlog Index Page在 wagtail 管理员中(请参阅下面的 models.py),由于某种原因,我移动的页面从管理员中消失了,所以我试图通过使用 ./manage.py sycndb 再次创建我的数据库来重复我所做的步骤。和 ./manage.py migrate ,再次创建页面,现在 url 不再显示。

我在 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"),
]

最佳答案

通常,您应该将页面创建为主页的子页面。在内部,这意味着您的博客索引页面将收到 /home/blog/ 的 url_path,并且由于 /home/ 映射到 http://localhost 在 root_paths 列表中,这将为其提供 http://localhost/blog/ 的最终 URL。

如您所见,如果您在主页旁边的根级别创建页面,它们将存在于默认站点记录之外,因此它们将没有可路由的 URL。但是,您可以通过位于 http://localhost/django-admin/ 的 Django 管理界面设置其他站点 - 例如,如果您想在 http://blog.example.com/ 托管您的博客,您将为以您的博客索引页为根的域创建一个站点条目。

关于python - 提供 `none` url 和 `live` 状态的 wagtail 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24976561/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com