settings.py

DMP is configurable with options in your settings.py file.

Typical Settings

Normally, your settings file should contain the following:

TEMPLATES = [
    {
        'NAME': 'django_mako_plus',
        'BACKEND': 'django_mako_plus.MakoTemplates',
        'OPTIONS': {
        },
    },
    {
        'NAME': 'django',
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

All Available Settings

The following are the full list of available options. Each option is described in detail later in this document.

import os, shutil

# this dict of options is merged with the current project's TEMPLATES entry for DMP
DEFAULT_OPTIONS = {
    # the default app and page to render in Mako when the url is too short
    # if None (no default app), DMP will not capture short URLs
    'DEFAULT_APP': 'homepage',
    'DEFAULT_PAGE': 'index',

    # functions to automatically add variables to the params/context before templates are rendered
    'CONTEXT_PROCESSORS': [
        'django.template.context_processors.static',            # adds "STATIC_URL" from settings.py
        'django.template.context_processors.debug',             # adds debug and sql_queries
        'django.template.context_processors.request',           # adds "request" object
        'django.contrib.auth.context_processors.auth',          # adds "user" and "perms" objects
        'django.contrib.messages.context_processors.messages',  # adds messages from the messages framework
        'django_mako_plus.context_processors.settings',         # adds "settings" dictionary
    ],

    # identifies where the Mako template cache will be stored, relative to each template directory
    'TEMPLATES_CACHE_DIR': '__dmpcache__',

    # the default encoding of template files
    'DEFAULT_TEMPLATE_ENCODING': 'utf-8',

    # imports for every template
    'DEFAULT_TEMPLATE_IMPORTS': [
        # alternative syntax blocks within your Mako templates
        # 'from django_mako_plus import django_syntax, jinja2_syntax, alternate_syntax',

        # the next two lines are just examples of including common imports in templates
        # 'from datetime import datetime',
        # 'import os, os.path, re, json',
    ],

    # whether autoescaping of expressions is on or off
    'AUTOESCAPE': True,

    # the converter class to use for parameter conversion
    # this should be ParameterConverter or a subclass of it
    'PARAMETER_CONVERTER': 'django_mako_plus.converter.ParameterConverter',

    # whether to send the custom DMP signals -- set to False for a slight speed-up in router processing
    # determines whether DMP will send its custom signals during the process
    'SIGNALS': False,

    # static file providers (see "static file" docs for full options here)
    'CONTENT_PROVIDERS': [
        # adds JS context - this should normally be listed FIRST
        { 'provider':   'django_mako_plus.JsContextProvider' },

        # Sass compiler and link generator
        # { 'provider':   'django_mako_plus.CompileScssProvider',
        #   'sourcepath': lambda p: os.path.join(p.app_config.name, 'styles', p.template_relpath + '.scss'),
        #   'targetpath': lambda p: os.path.join(p.app_config.name, 'styles', p.template_relpath + '.scss.css'),
        #   'command':    lambda p: [ shutil.which('sass'), f'--load-path="{BASE_DIR}"', p.sourcepath, p.targetpath ] },
        # { 'provider':   'django_mako_plus.CssLinkProvider',
        #   'filepath':   lambda p: os.path.join(p.app_config.name, 'styles', p.template_relpath + '.scss.css') },

        # Less compiler and link generator
        # { 'provider':   'django_mako_plus.CompileLessProvider',
        #   'sourcepath': lambda p: os.path.join(p.app_config.name, 'styles', p.template_relpath + '.less'),
        #   'targetpath': lambda p: os.path.join(p.app_config.name, 'styles', p.template_relpath + '.less.css'),
        #   'command':    lambda p: [ shutil.which('lessc'), f'--source-map', p.sourcepath, p.targetpath ] },
        # { 'provider':   'django_mako_plus.CssLinkProvider',
        #   'filepath':   lambda p: os.path.join(p.app_config.name, 'styles', p.template_relpath + '.less.css') },

        # generic compiler and link generator (see DMP docs, same options as other entries here)
        # { 'provider':   'django_mako_plus.CompileProvider' },
        # { 'provider':   'django_mako_plus.LinkProvider' },

        # link generators for regular JS and CSS: app/scripts/*.js and app/styles/*.css
        { 'provider':   'django_mako_plus.CssLinkProvider' },
        { 'provider':   'django_mako_plus.JsLinkProvider' },

        # link generators for app/scripts/__bundle__.js (webpack bundler)
        # { 'provider':   'django_mako_plus.WebpackJsLinkProvider' },
    ],

    # webpack file discovery, used by `manage.py dmp_webpack` to generate __entry__.js files
    'WEBPACK_PROVIDERS': [
        # finders for app/scripts/*.js and app/styles/*.css
        { 'provider': 'django_mako_plus.JsLinkProvider' },
        { 'provider': 'django_mako_plus.CssLinkProvider' },
    ],

    # additional template dirs to search
    'TEMPLATES_DIRS': [
        # '/var/somewhere/templates/',
    ],
}

DEFAULT_APP, DEFAULT_PAGE

When the url doesn’t contain the app and/or page, such as http://www.yourserver.com/, DMP uses the default app and page specified in your settings.py variables: DEFAULT_APP and DEFAULT_PAGE.

In the following table, the default app is set to homepage and your default page is set to index.html:

URL App Page Notes
http://www.yourserver.com/ homepage index.html  
http://www.yourserver.com/account/ account index.html  
http://www.yourserver.com/login/ login index.html If login is one of your apps
http://www.yourserver.com/login/ homepage login.html If login is not one of your apps
http://www.yourserver.com/account/password/ account password.html  

CONTEXT_PROCESSORS

The context is the dictionary of variables sent into render(). When the context is created, it is run through several “processors” to add names like STATIC_URL, request, etc.

Read more about context processors in Django’s Template API documentation.

TEMPLATES_CACHE_DIR

When a template is first rendered, the Mako engine generates a regular Python file out of it. It is this generated Python file that actually runs to create your HTML.

This option sets the directory where these cached, generated files are located. It is relative to the app/templates directory of each app.

DEFAULT_TEMPLATE_ENCODING

DMP and the Mako engine will read your templates using this encoding. Those who are coding their files with something other than utf-8 should understand when and why it might need to be changed.

If you change this setting, be sure to run python3 manage.py dmp_cleanup so your cached templates get rebuilt.

DEFAULT_TEMPLATE_IMPORTS

Python’s standard libraries must be imported to be used within template code. For example, to use the random module in a template, you’d place something like this at the top:

::

<%! import random %>

A random number is ${ random.randint(1, 100) }.

To automatically import the random module or any other Python module (including those from your project or installed via pip), add them in the DEFAULT_TEMPLATE_IMPORTS list. When Mako renders your templates, it will include the imports automatically so you don’t have repeat yourself in every template.

If you change this setting, be sure to run python3 manage.py dmp_cleanup so your cached templates get rebuilt.

SIGNALS

This option sets whether DMP should trigger Django-style signals. See Signals for more information.

CONTENT_PROVIDERS

This is one of the primary functions of DMP. It sets up autodiscovery of your static files, such as JS and CSS. See Static Files for more information on these options.

WEBPACK_PROVIDERS

This option works in connection with python3 manage.py dmp_webpack. See Bundling for more information.

TEMPLATES_DIRS

In contrast with Django, DMP templates are closely connected to their apps. If you need to search for templates in additional locations (a.k.a. the Django way), add folder paths to this list. Django will search someapp/templates/ first and then fall back to any directories you list here.