LazerFriends
LazerFriends

Reputation: 421

Wagtail: Dynamically Choosing Template With a Default

I'm wondering if there is a way in Wagtail to enter a custom template path via CharField in a base model, and then establish a template in an inherited model that would be the default. For example:

base/models.py

class WebPage(Page):        
    template_path = models.CharField()

    def get_template(self, request):
        if self.template_path:
            template = template_path
        else:
            template = *something*

app/models.py

class MyWebPage(WebPage):

    template = 'default_template.html'

Ideally, I'd establish the template attribute in the MyWebPage model, and that would act as a default. However, the get_template method in the WebPage base model would supersede it, but only if it's not empty. Is any of this possible?

Upvotes: 1

Views: 1075

Answers (1)

Kalob Taulien
Kalob Taulien

Reputation: 1918

I was reading through the Wagtail Docs and found this page (http://docs.wagtail.io/en/v2.1.1/advanced_topics/third_party_tutorials.html) and on that page was an article about dynamic templating. This is the page that has it: https://www.coactivate.org/projects/ejucovy/blog/2014/05/10/wagtail-notes-dynamic-templates-per-page/

The idea is to set a CharField and let the user select their template. In the following example they're using a drop down, which might even be better for you.

class CustomPage(Page):

    template_string = models.CharField(max_length=255, choices=(
                                         (”myapp/default.html”, “Default Template”), 
                                         (”myapp/three_column.html”, “Three Column Template”,
                                         (”myapp/minimal.html”, “Minimal Template”)))
    @property
    def template(self):
        return self.template_string 

^ code is from the coactivate.org website, it's not mine to take credit for.

In the template property, you could check if not self.template_string: and set your default path in there.

Edit #1: Adding Page inheritance.

You can add a parent Page (the Base class) and modify that, then extend any other class with your new Base class. Here's an example:

class BasePage(Page):
    """This is your base Page class. It inherits from Page, and children can extend from this."""

    template_string = models.CharField(max_length=255, choices=(
                                         (”myapp/default.html”, “Default Template”), 
                                         (”myapp/three_column.html”, “Three Column Template”,
                                         (”myapp/minimal.html”, “Minimal Template”)))
    @property
    def template(self):
        return self.template_string 


class CustomPage(BasePage):
    """Your new custom Page."""

    @property
    def template(self):
        """Overwrite this property."""
        return self.template_string 

Additionally, you could set the BasePage to be an abstract class so your migrations don't create a database table for BasePage (if it's only used for inheritance)

Upvotes: 4

Related Questions