wagtailwagtail-snippet

How to get snippet admin URL


I'm trying to get the admin snippet URL for an object, for example, I have products listed as snippets and one of their URL's is:

http://localhost:8000/admin/snippets/blog/blogproduct/edit/18/

I would like to programmatically get that, for example:

product = BlogProduct.objects.get(id=18)
print(product.admin_edit_url)

# expected result: http://localhost:8000/admin/snippets/blog/blogproduct/edit/18/

I could manually build this as per below but I prefer to get it programmatically

print(f"http://localhost:8000/admin/snippets/blog/blogproduct/edit/{product.id}/")

Solution

  • Wagtail provides an AdminURLFinder class for this purpose:

    from wagtail.admin.admin_url_finder import AdminURLFinder
    finder = AdminURLFinder()
    product = BlogProduct.objects.get(id=18)
    print(finder.get_edit_url(product))