Ticket #632: database-loader.diff

File database-loader.diff, 4.6 KB (added by Jannis Leidel <jl@…>, 18 years ago)

Database loader based on http://code.google.com/p/django-databasetemplateloader/

  • django/template/loaders/database/__init__.py

     
     1# Wrapper for loading templates from the database.
     2
     3from django.template import TemplateDoesNotExist
     4from django.contrib.sites.models import Site
     5from django.conf import settings
     6from django.template.models import Template
     7
     8try:
     9    site = Site.objects.get_current()
     10except:
     11    site = None
     12
     13def load_template_source(template_name, template_dirs=None):
     14    """
     15    Loads templates from the database by querying the database field ``name``
     16    with a template path and ``sites`` with the current site.
     17    """
     18    if site is not None:
     19        try:
     20            t = Template.objects.get(name__exact=template_name, sites__pk=site.id)
     21            return (t.content, 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name))
     22        except:
     23            pass
     24    raise TemplateDoesNotExist, template_name
     25load_template_source.is_usable = True
  • django/template/loaders/database/models.py

     
     1from django.db import models
     2from django.core import validators
     3from django.contrib.sites.models import Site
     4from django.utils.translation import gettext_lazy as _
     5
     6class Template(models.Model):
     7    """
     8    Defines a template model for use with the database template loader.
     9    The field ``name`` is the equivalent to the filename of a static template.
     10    """
     11    name = models.CharField(_('name'), unique=True, maxlength=100, help_text=_("Example: 'flatpages/default.html'"))
     12    content = models.TextField(_('content'))
     13    sites = models.ManyToManyField(Site)
     14    creation_date = models.DateTimeField(_('creation date'), auto_now_add=True)
     15    last_changed = models.DateTimeField(_('last changed'), auto_now=True)
     16    class Meta:
     17        db_table = 'django_template'
     18        verbose_name = _('template')
     19        verbose_name_plural = _('templates')
     20        ordering = ('name',)
     21    class Admin:
     22        fields = ((None, {'fields': ('name', 'content', 'sites')}),)
     23        list_display = ('name', 'creation_date', 'last_changed')
     24        list_filter = ('sites',)
     25        search_fields = ('name','content')
     26
     27    def __str__(self):
     28        return self.name
  • docs/templates_python.txt

     
    538538    Just like ``app_directories`` above, but it loads templates from Python
    539539    eggs rather than from the filesystem.
    540540
     541``django.template.loaders.database.load_template_source``
     542    Loads templates from the database and is represented by a standard Django
     543    model living in `django/template/models.py`_.
     544   
     545    Installation:
     546   
     547    1. Put ``'django.template.loaders.database'``,  ``'django.contrib.admin'``
     548       and ``'django.contrib.sites'`` in your ``INSTALLED_APPS`` setting.
     549    2. Add ``'django.template.loaders.database.load_template_source' to
     550        ``TEMPLATE_LOADERS``
     551    2. Run the command ``manage.py syncdb``.
     552       
     553    .. _INSTALLED_APPS: ../settings/#installed-apps
     554    .. _TEMPLATE_LOADERS: ../settings/#template-loaders
     555   
     556    ``manage.py syncdb`` creates two tables in your database:
     557    ``django_template`` and ``django_template_sites``. ``django_template`` is
     558    a simple lookup table that maps a template name to the text content of the
     559    template. The template name is the equivalent to the filename in the
     560    filesystem template loader. ``django_template_sites`` associates a
     561    template with a site.
     562   
     563    Templates are loaded by querying the database field ``name`` with a
     564    template path as if they were static files. A template with the name
     565    ``foo.html`` can be loaded with ``get_template('foo.html')``.
     566   
     567    You can find a new "Template" section on the admin index page. Edit
     568    templates as you edit any other object in the system.
     569
     570    You can access template database objects via the `Django database API`_.
     571
     572    .. _Django model: ../model_api/
     573    .. _django/template/loaders/database/models.py: http://code.djangoproject.com/browser/django/trunk/django/template/loaders/database/models.py
     574    .. _Django database API: ../db_api/
     575
    541576Django uses the template loaders in order according to the ``TEMPLATE_LOADERS``
    542577setting. It uses each loader until a loader finds a match.
    543578
Back to Top