allenrabinovich
allenrabinovich

Reputation: 484

How can include a regular Django view content in a Wagtail page?

I have a website running on Django that uses Wagtail for most of the pages (that are simply text content, editable through the Wagtail Admin UI). However, I have some content that needs to be dynamically rendered from my own Django model (let's say it's a list of handouts). I'd like that content to be rendered on a Wagtail page that would also have some Wagtail models (e.g., top menu, some introductory text). What's the best way to do this? I thought about making the list of handouts an API endpoint, and rendering it with JavaScript + XHR, but it seems like there should be a good way to do it server-side.

Upvotes: 1

Views: 677

Answers (2)

Colin Wong
Colin Wong

Reputation: 56

One option would be to create a property on your Page model that retrieves whatever you are looking for from this other model

# rest of your imports
...
from handouts import Handouts

class MyPage(Page):
# rest of your page's fields
...
    @property
    def get_handouts(self):
        handouts = Handouts.objects.all()
        return handouts

Upvotes: 0

Lucas Moeskops
Lucas Moeskops

Reputation: 5443

Assuming the introductionary text is a block in a Wagtail StreamField, you could also define a Wagtail Block that links to a Django model. https://pypi.org/project/wagtail-modelchooser/ is a useful extension that provides this functionality. This makes it possible to render things from Django model instances in your Wagtail stream content.

Upvotes: 1

Related Questions