Ticket #632: dbtemplates.diff
File dbtemplates.diff, 4.7 KB (added by , 18 years ago) |
---|
-
django/contrib/dbtemplates/models.py
1 from django.db import models 2 from django.core import validators 3 from django.contrib.sites.models import Site 4 from django.utils.translation import gettext_lazy as _ 5 6 class 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 -
django/template/loaders/database.py
1 # Wrapper for loading templates from the database. 2 3 from django.conf import settings 4 from django.template import TemplateDoesNotExist 5 from django.contrib.dbtemplates.models import Template 6 from django.contrib.sites.models import Site 7 8 try: 9 site = Site.objects.get_current() 10 except: 11 site = None 12 13 def 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 25 load_template_source.is_usable = True -
docs/templates_python.txt
538 538 Just like ``app_directories`` above, but it loads templates from Python 539 539 eggs rather than from the filesystem. 540 540 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.contrib.dbtemplates'``, ``'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/contrib/dbtemplates/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/dbtemplates/models.py 574 .. _Django database API: ../db_api/ 575 541 576 Django uses the template loaders in order according to the ``TEMPLATE_LOADERS`` 542 577 setting. It uses each loader until a loader finds a match. 543 578