Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Wagtail:使用默认值动态选择模板_Python_Django_Wagtail - Fatal编程技术网

Python Wagtail:使用默认值动态选择模板

Python Wagtail:使用默认值动态选择模板,python,django,wagtail,Python,Django,Wagtail,我想知道Wagtail中是否有一种方法可以通过基本模型中的CharField输入自定义模板路径,然后在继承模型中建立一个模板,这将是默认的。例如: base/models.py class WebPage(Page): template_path = models.CharField() def get_template(self, request): if self.template_path: template = t

我想知道Wagtail中是否有一种方法可以通过基本模型中的CharField输入自定义模板路径,然后在继承模型中建立一个模板,这将是默认的。例如:

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*
class MyWebPage(WebPage):

    template = 'default_template.html'
app/models.py

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

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

    template = 'default_template.html'

理想情况下,我会在MyWebPage模型中建立模板属性,这将作为默认设置。但是,网页基本模型中的get_template方法将取代它,但前提是它不是空的。这有可能吗?

我在阅读Wagtail文档时发现了这一页,在这一页上有一篇关于动态模板的文章。这是包含它的页面:

其想法是设置一个CharField,让用户选择他们的模板。在下面的示例中,他们使用了一个下拉列表,这可能对您更有利

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 
^代码来自coactivate.org网站,这不是我的功劳

在template属性中,您可以检查self.template\u string:是否正确,并在其中设置默认路径

编辑1: 添加页面继承

您可以在基类中添加一个父页面并对其进行修改,然后使用新基类扩展任何其他类。下面是一个例子:

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 

此外,您可以将BasePage设置为一个抽象类,这样您的迁移就不会为BasePage创建数据库表(如果它仅用于继承)

是的,谢谢,我确实看到了该文档。我希望这样做的方式是,模板属性在基类中,默认值在祖先类中设置。因此,我可以在所有的祖先类中重复这段代码,但我当然希望避免这样做。