route定制路线模板

时间:2019-01-14 10:26:14

标签: django wagtail

使用RoutablePageMixin时如何向页面添加自定义模板?

@route(r'^tag/(?P<tag>[-\w]+)/$')
def post_by_tag(self, request, tag, *args, **kwargs):
    self.search_type = 'tag'
    self.search_term = tag
    self.posts = self.get_posts().filter(tags__slug=tag)
    return Page.serve(self, request, *args, **kwargs)

1 个答案:

答案 0 :(得分:3)

调用django.shortcuts.render函数,就像在a standard Django view function中一样:

from django.shortcuts import render

@route(r'^tag/(?P<tag>[-\w]+)/$')
def post_by_tag(self, request, tag, *args, **kwargs):
    # ...
    posts = self.get_posts().filter(tags__slug=tag)
    return render(request, 'path/to/template.html', {
        'page': self,
        'posts': posts,
    })