Django Tip:Apple-specfic Templates


もっと読む
原文の住所:http://www.codekoala.com/blog/2009/django-tip-application-specific-templates/
Today I have another Django gooddie to share with you.For the past few days、I've been ststugling to come uuuup with a way to only load certain Django template(libriririririries when a papapartularar Djajaggggggggjajajajajaggggggggggggjajajajajajajajagggggggaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaapariririririricacacacacacacacacacacacacacacacacacaings to the pile.
 
ウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティティファファファファファファファファファファファファファファファファファファファファファファファファファファファファファway、as opposed to overriding templates on a per-site basis.
The Problem
ウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェウェテーテーテーテーテーテーテーテーテーテーテーテーテーテーテーテーテーテーテーテーテーテーテート「Conttet B」ift instaled.Thiswoisworks great all the uuuuntitititititil the ininininininininininininininap uuuuuuuuunininininininitytytytytytytytyuuuuuuuuntititititititititititil neneneed inininininininininininininy're actually rended.That means that if foo is not installed and one of your template s included a template(from foo's template)は、Django Will complane bencause it cannot find that(since fo is not in your sent stant)ED.APPS)
 
I investigated several possible solutions to this、including a custom loadifp(.The idea was to only load a template(library the specified appication exists in settings.INstaLLED_)APPS.This proved to be n interesting and very,very hacky endeavor.In the end it didn't work,and it was taring muctoo long to get anywhere useful.
The Solution
The solution I came up with for this situation is to create an additional include(.I baically copied the include the from Django it a bit.The reult:
 
from django import template
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_app
from django.template.loader_tags import ConstantIncludeNode, IncludeNode

register = template.Library()

def do_include_ifapp(parser, token):
    """
    Loads a template and renders it with the current context if the specified
    application is in settings.INSTALLED_APPS.

    Example::

        {% includeifapp app_label "foo/some_include" %}
    """
    bits = token.split_contents()
    if len(bits) != 3:
        raise TemplateSyntaxError, "%r tag takes two argument: the application label and the name of the template to be included" % bits[0]

    app_name, path = bits[1:]
    app_name = app_name.strip('"\'')
    try:
        models = get_app(app_name)
    except ImproperlyConfigured:
        return template.Node()

    if path[0] in ('"', "'") and path[-1] == path[0]:
        return ConstantIncludeNode(path[1:-1])
    return IncludeNode(path)
register.tag('includeifapp', do_include_ifapp)
 
The magic here is the return template.Node()if Django cannot load a particular aplication.This makes it so the template you would be including will not be parsed、and the invalid template
 
To use this(in your Django-powered site、simple plug it one of your template(libries and do something like this)
 
{% extends 'base.html' %}
{% load our_global_tags %}

{% block content %}

Global Content Header

Bla bla

{% includeifapp foo 'foo_specific_junk.html' %} {% endblock %}
 
And within
foo_specific_junnk.you would
load whatemplate(libries you need that would break your templates without)
foo being installed.This(shoud work for any appplication.I would be interested to hear what you use it for in the comments!