Database template loader

 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. DRY with common model fields (another way) by jmrbcu 6 years, 9 months ago
  2. email_links by sansmojo 6 years, 10 months ago
  3. Improved Pickled Object Field by taavi223 4 years, 8 months ago
  4. FieldsetForm by Ciantic 7 years ago
  5. Pickled Object Field by obeattie 6 years, 4 months ago

Comments

doomatel (on April 1, 2012):

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

#

(Forgotten your password?)