Setup
=====

1. Get the source from the `Git repository`_ or install it from the
   Python Package Index by running ``pip install django-dbtemplates``.
2. Follow the instructions in the INSTALL file
3. Edit the settings.py of your Django site:

   * Add ``dbtemplates`` to the ``INSTALLED_APPS`` setting

     Check if ``django.contrib.sites`` and ``django.contrib.admin`` are in
     ``INSTALLED_APPS`` and add if necessary.

     It should look something like this::

         INSTALLED_APPS = (
             'django.contrib.auth',
             'django.contrib.contenttypes',
             'django.contrib.sessions',
             'django.contrib.sites',
             'django.contrib.admin',
             'django.contrib.flatpages',
             # ..
             'dbtemplates',
         )

   * Add ``dbtemplates.loader.Loader`` to the ``TEMPLATE_LOADERS`` list
     in the settings.py of your Django project.

     It should look something like this::

       TEMPLATE_LOADERS = (
           'django.template.loaders.filesystem.Loader',
           'django.template.loaders.app_directories.Loader',
           'dbtemplates.loader.Loader',
       )

     Order of TEMPLATE_LOADERS is important. In the former example, templates from database
     will be used as a fallback (ie. when template does not exists in other locations). 
     If you want template from database to be used to override templates in other locations,
     put ``dbtemplates.loader.Loader`` at beginning of ``TEMPLATE_LOADERS`` settting.

4. Sync your database ``python manage.py syncdb``
5. Restart your Django server

.. _Git repository: http://github.com/jezdez/django-dbtemplates/

Usage
=====

Creating database templates is pretty simple: Just open the admin interface
of your Django-based site in your browser and click on "Templates" in the
"Dbtemplates" section.

There you only need to fill in the ``name`` field with the identifier, Django
is supposed to use while searching for templates, e.g.
``blog/entry_list.html``. The ``content`` field should be filled with the
content of your template.

Optionally, by leaving the ``content`` field empty you are able to tell
``dbtemplates`` to look for a template with the ``name`` by using Django's
other template loaders. For example, if you have a template called
``blog/entry_list.html`` on your file system and want to save the templates
contents in the database, you just need to leave the content field empty to
automatically populate it. That's especially useful if you don't want to
copy and paste its content manually to the textarea.

Example
=======

``dbtemplates`` comes with an example Django project that let's you try it 
out. The example uses Django's own `flatpages app`_ to enable you to create
a simple page using ``dbtemplates``. Flat pages are a perfect fit to
dbtemplates since they come prepackaged and are simple to use.

Here is how it works:

1. Open your command line and change to the ``example`` directory in the
   directory with the extracted source distribution.
2. Run ``python manage.py syncdb`` and follow the instructions.
3. Run ``python manage.py runserver`` and open your favorite browser with the
   address http://127.0.0.1:8000/admin/.
4. Next add a new `Template` object in the ``dbtemplates`` section and use
   ``flatpages/default.html`` as the value for the ``name`` field. For the
   ``content`` field use this example::

        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
        <html>
        <head>
        <title>{{ flatpage.title }}</title>
        </head>
        <body>
        {{ flatpage.content }}
        </body>
        </html>

5. Return to the home screen of the admin interface and add a new flat page.
   Use ``/`` (yep, just a forward slash) and whatever ``title`` and 
   ``content`` you prefer. Please make sure you select the default site
   ``example.com`` before you save the flat page.
6. Visit http://127.0.0.1:8000/ and see the flat page you just created 
   rendered with the ``flatpages/default.html`` template provided by  
   ``dbtemplates``.

.. _flatpages app: http://docs.djangoproject.com/en/dev/ref/contrib/flatpages/

