Login

Database template loader

Author:
doomatel
Posted:
March 30, 2012
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

This snippet provides getting templates from the model in database.

We work with templates as usual (using as a template name value of the field "slug").

You can do your own application without "TemplateTypes" model - it's added for ability to filter templates. You can use choices or remove "template_type" field and "TemplateTypes" model at all.

For ease of editing, you can connect all this to the admin interface, adding to the field "template_body" widget with syntax highlighting (I used CodeMirror).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# your_app/models.py:

from django.contrib.contenttypes.models import ContentType
from django.db import models

class TemplateTypes(models.Model):

    type_name = models.CharField(max_length=80)
    content_type = models.ForeignKey(ContentType, blank=True, null=True)
    
    def __unicode__(self):
        return self.type_name

class Templates(models.Model):

    slug = models.CharField(max_length=80)
    template_type = models.ForeignKey('TemplateTypes')
    template_body = models.TextField()
    
    def __unicode__(self):
        return self.slug


# your_app/loaders.py:

from django.template import loader, TemplateDoesNotExist
from models import *

class DatabaseLoader(loader.BaseLoader):

    is_usable = True

    def load_template_source(self, template_name, template_dirs=None):
        try:
            return Templates.objects.get(slug=template_name).template_body, template_name
        except Templates.DoesNotExist:
            raise TemplateDoesNotExist, template_name


# your_project/settings.py

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'your_project.your_app.loaders.DatabaseLoader',
)


# your_app/admin.py (OPTIONAL):

from django.conf import settings
from django.contrib import admin
from django.forms import ModelForm
from models import *

class TemplatesAdminForm(ModelForm):

    class Media:

        css = {
            'all': (
                '%syour_app/css/codemirror.css' % settings.STATIC_URL,
            )
        }

        js = (
            '%syour_app/js/jquery.min.js' % settings.STATIC_URL,
            '%syour_app/js/codemirror.js' % settings.STATIC_URL,
            '%syour_app/js/xml.js' % settings.STATIC_URL,
            '%syour_app/js/javascript.js' % settings.STATIC_URL,
            '%syour_app/js/css.js' % settings.STATIC_URL,
            '%syour_app/js/htmlmixed.js' % settings.STATIC_URL,
            '%syour_app/js/htmlembedded.js' % settings.STATIC_URL,
            '%syour_app/js/admin-connector.js' % settings.STATIC_URL,
        )

class TemplatesAdmin(admin.ModelAdmin):

    form = TemplatesAdminForm

admin.site.register(Templates, TemplatesAdmin)


# your_app/static/your_app/js/admin-connector.js (OPTIONAL):

$(document).ready(function(){
    var editor = CodeMirror.fromTextArea(document.getElementById("id_template_body"), {
        lineNumbers: true,
        matchBrackets: true,
        mode: "application/x-ejs",
        indentUnit: 4,
        indentWithTabs: true,
        enterMode: "keep",
        tabMode: "shift"
    });
});

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 9 months, 4 weeks ago
  5. Help text hyperlinks by sa2812 10 months, 3 weeks ago

Comments

doomatel (on April 1, 2012):

I didn't know about this application. Thanks for the tip.

#

Please login first before commenting.