虽然我认为您的方法没有问题,但我认为通用模板标签将提供最大的灵活性,特别是如果您想将此功能扩展到以后可能安装的其他应用程序。
您的基本模板会加载一个通用的“boxes”标签。在标签的源代码中,您可以根据为该特定实例安装的应用程序呈现您想要的任何内容。因此,您可以拥有一组默认应用程序来渲染框,或者最终用户可以自定义哪些应用程序应该渲染框。
在您的设置、配置甚至标签本身中,您可以确定要为每个应用程序块呈现的模板。
假设每个应用程序在app/templates
目录中都有它的模板 - 这个伪程序应该让你继续前进(这是未经测试的):
from django.conf import settings
from django import template
register = template.Library()
class GenericBox(template.Node):
def __init__(self, app):
self.app = app
def render(self, context):
if self.app not in settings.INSTALLED_APPS:
return '' # if the app is not installed
# Here you would probably do a lookup against
# django.settings or some other lookup to find out which
# template to load based on the app.
t = template.loader.get_template('some_template.html') # (or load from var)
c = template.RequestContext(context,{'var':'value'}) # optional
return t.render(c)
@register.tag(name='custom_box', takes_context=True, context_class=RequestContext)
def box(parser, token):
parts = token.split_contents()
if len(parts) != 2:
parts[1] = 'default_app' # is the default app that will be rendered.
return GenericBox(parts[1])