5

I have a Django site which contains a blog. To get it up and running quickly I just added some static blog posts using Django's TemplateView.

I have now added Wagtail and set up a blog platform using that.

The problem I now have is linking to blog posts from elsewhere in the site. Is there a way to reverse the urls for teh blog posts created in Wagtail (using the slug)?

halfer
  • 19,824
  • 17
  • 99
  • 186
wobbily_col
  • 11,390
  • 12
  • 62
  • 86

2 Answers2

9

See either

  • pageurl to reverse a URL given a Page object {% pageurl page_object %}, or
  • slugurl to reverse a URL given a Page object's slug {% slugurl some_slug %}

If you have a Page object, you can also use {{ page_object.url }}, but that doesn't give you the nice multiple-site behaviour.

Florian
  • 2,562
  • 5
  • 25
  • 35
nimasmi
  • 3,978
  • 1
  • 25
  • 41
2

For those who want to reverse wagtail admin urls and create menu link on wagtail admin page to make creation of blogs more comfortable

from wagtail.admin.menu import MenuItem
from wagtail.core import hooks # for wagtail v 2.15
# from wagtail import hooks # for wagtail v3

from django.urls import reverse
from django.contrib.admin.utils import quote


@hooks.register('register_admin_menu_item')
def register_news_menu_item():
    page = BlogIndexPage.objects.first()
    blogs_url = reverse('wagtailadmin_explore', args=(quote(page.pk),))
    return MenuItem('Blogs', blogs_url, icon_name='doc-empty-inverse', order=1)
Arseniy Lebedev
  • 440
  • 4
  • 9