Index: django/contrib/dbtemplates/sync_templates.py
===================================================================
--- django/contrib/dbtemplates/sync_templates.py	(Revision 0)
+++ django/contrib/dbtemplates/sync_templates.py	(Revision 0)
@@ -0,0 +1,80 @@
+"""
+Helper function for syncing templates in TEMPLATES_DIRS with the dbtemplates
+contrib app.
+"""
+
+from django.conf import settings
+from django.template import TemplateDoesNotExist
+from django.contrib.dbtemplates.models import Template
+from django.contrib.sites.models import Site
+
+import os
+import sys
+
+try:
+    site = Site.objects.get_current()
+except:
+    site = None
+
+def synctemplates(extension=".html", overwrite=False):
+    """
+    Helper function for syncing templates in TEMPLATES_DIRS with the
+    dbtemplates contrib app.
+    """
+    tried = []
+    synced = []
+    existing = []
+    overwritten = []
+    
+    if site is not None:
+        for template_dir in settings.TEMPLATE_DIRS:
+            if os.path.isdir(template_dir):
+                for dirpath, subdirs, filenames in os.walk(template_dir):
+                    for file in filenames:
+                        if file.endswith(extension) and not file.startswith("."):
+                            filepath = os.path.join(dirpath, file)
+                            filename = filepath.split(template_dir)[1][1:]
+                            try:
+                                try:
+                                    t = Template.objects.get(name__exact=filename)
+                                except Template.DoesNotExist:
+                                    filecontent = open(filepath, "r").read()
+                                    t = Template(name=filename, content=filecontent)
+                                    t.save()
+                                    t.sites.add(site)
+                                    synced.append(filename)
+                                else:
+                                    if overwrite:
+                                        t.content = open(filepath, "r").read()
+                                        t.save()
+                                        t.sites.add(site)
+                                        overwritten.append(t.name)
+                                    else:
+                                        existing.append(t.name)
+                            except IOError:
+                                tried.append(filepath)
+                            except:
+                                raise TemplateDoesNotExist
+
+        if len(existing) > 0:
+            print "\nAlready existing templates:"
+            for _existing in existing:
+                print _existing
+
+        if len(overwritten) > 0:
+            print "\nOverwritten existing templates:"
+            for _replaced in overwritten:
+                print _replaced
+
+        if len(synced) > 0:
+            print "\nSuccessfully synced templates:"
+            for _synced in synced:
+                print _synced
+
+        if len(tried) > 0:
+            print "\nTried to sync but failed:"
+            for _tried in tried:
+                print _tried
+
+if __name__ == "__main__":
+    synctemplates()
Index: django/contrib/dbtemplates/__init__.py
===================================================================
Index: django/contrib/dbtemplates/models.py
===================================================================
--- django/contrib/dbtemplates/models.py	(Revision 0)
+++ django/contrib/dbtemplates/models.py	(Revision 0)
@@ -0,0 +1,28 @@
+from django.db import models
+from django.core import validators
+from django.contrib.sites.models import Site
+from django.utils.translation import gettext_lazy as _
+
+class Template(models.Model):
+    """
+    Defines a template model for use with the database template loader.
+    The field ``name`` is the equivalent to the filename of a static template.
+    """
+    name = models.CharField(_('name'), unique=True, maxlength=100, help_text=_("Example: 'flatpages/default.html'"))
+    content = models.TextField(_('content'))
+    sites = models.ManyToManyField(Site)
+    creation_date = models.DateTimeField(_('creation date'), auto_now_add=True)
+    last_changed = models.DateTimeField(_('last changed'), auto_now=True)
+    class Meta:
+        db_table = 'django_template'
+        verbose_name = _('template')
+        verbose_name_plural = _('templates')
+        ordering = ('name',)
+    class Admin:
+        fields = ((None, {'fields': ('name', 'content', 'sites')}),)
+        list_display = ('name', 'creation_date', 'last_changed')
+        list_filter = ('sites',)
+        search_fields = ('name','content')
+
+    def __str__(self):
+        return self.name
Index: django/contrib/dbtemplates/loader.py
===================================================================
--- django/contrib/dbtemplates/loader.py	(Revision 0)
+++ django/contrib/dbtemplates/loader.py	(Revision 0)
@@ -0,0 +1,25 @@
+# Wrapper for loading templates from the database.
+
+from django.conf import settings
+from django.template import TemplateDoesNotExist
+from django.contrib.dbtemplates.models import Template
+from django.contrib.sites.models import Site
+
+try:
+    site = Site.objects.get_current()
+except:
+    site = None
+
+def load_template_source(template_name, template_dirs=None):
+    """
+    Loads templates from the database by querying the database field ``name``
+    with a template path and ``sites`` with the current site.
+    """
+    if site is not None:
+        try:
+            t = Template.objects.get(name__exact=template_name, sites__pk=site.id)
+            return (t.content, 'db:%s:%s' % (settings.DATABASE_ENGINE, template_name))
+        except:
+            pass
+    raise TemplateDoesNotExist, template_name
+load_template_source.is_usable = True
Index: docs/dbtemplates.txt
===================================================================
--- docs/dbtemplates.txt	(Revision 0)
+++ docs/dbtemplates.txt	(Revision 0)
@@ -0,0 +1,76 @@
+============================
+The database template loader
+============================
+
+Loads templates from the database and is represented by a standard Django
+model living in `django/contrib/dbtemplates/models.py`_.
+
+Installation
+============
+
+To install the database template loader, follow these steps:
+
+    1. Put ``'django.contrib.dbtemplates'``,  ``'django.contrib.admin'``
+       and ``'django.contrib.sites'`` in your ``INSTALLED_APPS`` setting.
+    2. Add ``'django.contrib.dbtemplates.loader.load_template_source'`` to 
+       ``TEMPLATE_LOADERS``
+    3. Run the command ``manage.py syncdb``.
+    
+.. _INSTALLED_APPS: ../settings/#installed-apps
+.. _TEMPLATE_LOADERS: ../settings/#template-loaders
+
+How it works
+============
+
+``manage.py syncdb`` creates two tables in your database:
+``django_template`` and ``django_template_sites``. ``django_template`` is
+a simple lookup table that maps a template name to the text content of the
+template. The template name is the equivalent to the filename in the
+filesystem template loader. ``django_template_sites`` associates a
+template with a site.
+
+Templates are loaded by querying the database field ``name`` with a
+template path as if they were static files. A template with the name
+``foo.html`` can be loaded with ``get_template('foo.html')``.
+
+How to add, change and delete templates
+=======================================
+
+Via the admin interface
+-----------------------
+
+You can find a new "Template" section on the admin index page. Edit
+templates as you edit any other object in the system.
+
+Via the Python API
+------------------
+
+Templates are represented by a standard `Django model`_, which lives in
+`django/contrib/dbtemplates/models.py`_. You can access template database
+objects via the `Django database API`_.
+
+.. _Django model: ../model_api/
+.. _django/contrib/dbtemplates/models.py: http://code.djangoproject.com/browser/django/trunk/django/contrib/dbtemplates/models.py
+.. _Django database API: ../db_api/
+
+Via the included syncing utility
+--------------------------------
+
+If you want to add all filesystem based templates from ``TEMPLATES_DIRS``
+to the database at once, you can use the ``sync_templates.py`` utility.
+Just run this command::
+
+    python /path/to/django/contrib/dbtemplates/sync_templates.py
+
+Make sure to substitute ``/path/to/`` with the path to the Django codebase on
+your filesystem.
+
+Alternatively you can import the function ``synctemplates`` from
+``django.contrib.dbtemplates.sync_templates`` from the Django shell and pass
+the following parameters:
+
+    ``extension`` String:
+        defines the extension of the files to import, defaults to ".html"
+
+    ``overwrite`` Boolean:
+        overwrites existing templates when True, defaults to False
