g:调用routablepageurl引发AttributeError的模板标记

时间:2018-07-30 15:29:13

标签: python django wagtail

我正在尝试在Wagtail中构建一个简单的博客,并试图弄清楚其中的所有内容如何组合在一起以及为什么事情不起作用使我无所适从。当我尝试加载“关于”页面时,我不断得到AttributeError: 'str' object has no attribute 'relative_url'的信息,我认为这与我将{include]带入navbar.html的{​​{1}}有关。我无法理解错误,任何人都可以帮助将其分解吗?

Navbar.html

base.html

Models.py

{% load static wagtailcore_tags blogapp_tags wagtailroutablepage_tags %}

<div class="container" id="nav-container">
  <nav class="navbar navbar-expand-sm py-0 px-0" id="main-nav">
  <button class="navbar-toggler" data-toggle="collapse" data-target="#navbarCollapse">
    <span class="navbar-toggler-icon"></span>
  </button>
  <form role="search" method="get" class="form-inline pt-3 form-search" action="{% routablepageurl home_page 'post_search' %}">
    <input type="text" name="q" id="search-bar" class="form-control mr-sm-2 search-query" type="search" placeholder="Search&hellip;" title="Search for:" aria-label="Search">
    <button class="btn btn-outline-danger my-2 my-sm-0" name="submit" id="search-button" type="submit" value="Search">Search</button>
  </form>
  <div class="collapse navbar-collapse" id="navbarCollapse">
    <ul class="navbar-nav mt-2 ml-auto">
      <li class="nav-item">
        <a href="/about" class="nav-link">About</a>
      </li>
      <li class="nav-item">
        <a href="#" class="nav-link">Archive</a>
      </li>
    </ul>
  </div>
  </nav> 
</div>

about_page.html

class HomePage(RoutablePageMixin, Page):
    description = models.CharField(max_length=255, blank=True, null=True)

    content_panels = Page.content_panels + [
        FieldPanel('description', classname='full')
    ]

    def get_context(self, request, *args, **kwargs):
        context = super(HomePage, self).get_context(request, *args, **kwargs)
        context['posts'] = self.posts
        context['home_page'] = self
        context['search_type'] = getattr(self, 'search_type', '')
        context['search_term'] = getattr(self, 'search_term', '')
        return context


    def get_posts(self):
        return BlogPage.objects.descendant_of(self).live()

...

    @route(r'^search/$')
    def post_search(self, request, *args, **kwargs):
        search_query = request.GET.get('q', None)
        self.posts = self.get_posts()
        if search_query:
            self.posts = self.posts.filter(body__icontains=search_query)
            self.search_term = search_query
            self.search_type = 'search'
        return Page.serve(self, request, *args, **kwargs)


class AboutPage(Page):
    body = RichTextField(blank=True)

    content_panels = Page.content_panels + [
        FieldPanel('body', classname='full')
    ]

此处的完整项目:

https://bitbucket.org/nicecore/adiscaday-wagtail/src/master/

此处的完整追溯:

http://dpaste.com/1T65ZCV

2 个答案:

答案 0 :(得分:1)

网站的根页面始终作为变量request.site.root_page可用,因此,您不必将home_page定义为所创建的每种页面类型,而是可以将标签更改为:

{% routablepageurl request.site.root_page.specific 'post_search' %}

答案 1 :(得分:0)

@gasman在我的原始帖子的评论中有帮助地建议home_page变量被某种方式覆盖,因此我从{{1}中的get_context模型中复制了BlogPage方法}(开始怀疑我是否不应该仅在我的“关于”页面上使用AboutPage的实例,而将不需要的字段留空...),它似乎正在起作用:

BlogPage

对于class AboutPage(Page): body = RichTextField(blank=True) content_panels = Page.content_panels + [ FieldPanel('body', classname='full') ] def get_context(self, request, *args, **kwargs): context = super(AboutPage, self).get_context(request, *args, **kwargs) context['home_page'] = self.get_parent().specific context['about'] = self return context 被覆盖的原因的任何见解,以便将来可以避免出现此问题,将不胜感激!