如何在Wa中创建不可见的虚拟页面? [英] How to create an invisible dummy page in Wagtail?
问题描述
如何在Wa中创建不可见的虚拟页面?
How can I create an invisible dummy page in Wagtail?
我需要在Wagtail中创建一个虚拟"页面对象,以便为基于非Wagtail的页面以及外部资源构建菜单. (请参阅我的帖子
I need a "virtual" page object within Wagtail to build a menu for non Wagtail based pages and also external resources. (See my entry post here)
class MenuDummyPage(Page):
menu_description = models.CharField(max_length=255, blank=True)
menu_icon = models.CharField(max_length=255, blank=True)
menu_link = models.CharField(max_length=255, blank=True)
settings_panels = [
FieldPanel('menu_description'),
FieldPanel('menu_icon'),
FieldPanel('menu_link'),
]
def get_sitemap_urls(self):
return []
def serve(self, request):
pass
如果我创建了上述页面对象,则该对象未在生成的wagtail网站地图中列出.
If I create the above page object then it is not listed within the generated wagtail sitemap.
但是,如果我自己手动导航到该页面,则将调用该对象.我该如何阻止呢?
But if I navigate on my own to that page manually the object is called. How can I stop this?
示例: 如果我创建一个标题为"This is a test"的MenuDummyPage,则系统将自动生成一个slug =>"this-is-a-test".
Example: If I create a MenuDummyPage with the title "This is a test" then the system will automatically generate a slug => "this-is-a-test".
如果我在浏览器中调用"/this-is-a-test"/,则ag会回答,因为the存在.如何为我的"MenuDummyPage"对象删除此行为?
If I call "/this-is-a-test"/ in my browser wagtail is answering because the slug exists. How can I remove this behavior for my "MenuDummyPage" objects?
推荐答案
如果虚拟页面的意思是在页面树中保留了其他页面的页面,则可以执行以下操作:
If what you mean by a dummy page is a page under which other pages are kept in the page tree, then you could do the following:
from django.http import HttpResponseRedirect
class Node(Page):
subpage_types = [your subpage types]
parent_page_types = [your parent page types]
link = models.CharField(max_length=255, default='', blank='True')
content_panels = Page.content_panels + [
FieldPanel('link')
]
def serve(self, request):
if self.link is not None:
return HttpResponseRedirect(self.link)
# To handle the situation where someone inadvertantly lands on a parent page, do a redirect
first_descendant = self.get_descendants().first()
if first_descendant:
return HttpResponseRedirect(first_descendant.url)
else:
return HttpResponseRedirect(request.site.root_page.url)
可选的link
字段允许您在页面树结构中的该位置定义链接.再次,以上假设您将基于Page
的项目用作Page
树中的占位符,以便可以在其下放置其他Page
.只要您没有在模板中呈现此页面的URL,那么用户就永远不会知道如何获取Node
的URL,但是如果有人确实获取了Node
类型的页面的URL,则,然后first_descendant
逻辑通过将它们发送到Node
的第一个后代或如果没有Node
的后代发送到首页来处理这种情况.
The optional link
field allows you to define a link if you wish for this position in the page tree structure. The above, again, assumes that you are using this Page
-based item as a placeholder in the Page
tree so that you can have other Page
s under it. As long as you don't render the url of this page in your template, then users should never know how to get to the url for the Node
, but if someone does get to the url for a Node
-type page, then the first_descendant
logic handles this situation by sending them to either the first descendant of the Node
or to the home page if no descendants of Node
exist.
在模板中(请注意使用 specific_class
):
In your template (note the use of specific_class
):
{% for item in menu_items %}
<li>
<a href="{% if item.specific.link and item.specific.link != '' %}{{ item.specific.link }}{% elif item.specific_class == 'Node'%}#{% else %}{% pageurl item %}{% endif %}">{{ item.title }
</a>
</li>
{% endfor %}
这篇关于如何在Wa中创建不可见的虚拟页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!