The simple, elegant Django Mako library Used base engine to create a template rendering class to be used like Django's TemplateView class. To understand how to use it, read Custom backends on django.
- The current implementation assumes all system templates are Mako Template. Thus, when you start a new template make sure that the template language is Mako not Django.
- If you want to use another template backend like Django Template Backend, just pass
using='Django'
in your FBV or addtemplate_engine = 'mako'
in your CBV.
Enjoy! This shouldn't be tricky any more.
To install the package as a requirement in your python environemnt just do
pip install djangomako
After installing the package in your python environment, navigate to
your project's settings.py
and add the following lines in the
TEMPLATES
variable
TEMPLATES = [
# ...
{
'BACKEND': 'djangomako.backends.MakoBackend',
'NAME': 'mako',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
},
# ...
]
- The
BACKEND
value is from this library. - The
NAME
is simply the template identifier. - In
DIRS
you're gonna include all the directories that have mako templates. - The order matters here, so if you want your project to support only mako, you just need to remove the Django entry from the templates, while if you need mako as a fallback only, then you need to put it under the Django Template entry.
I passed some template variables to the context if the request objects exists:
CSRF_TOKEN
andCSRF_INPUT
${ csrf_input } ## {% csrf_token %} in Django templates. ${ csrf_token } ## {{ csrf_token }} in Django templates.
- To access the request:
${ request }
- To include a static file url:
${ static('image.png') } ## {% static "image.png" %} in Django templates.
- To reverse a url in the template:
${ url('home') } ## {% url 'home' %} in Django templates.
An example of how to use this library in Class-Based view and Function-Based Views is inside niceapp app.
To test how this engine handles errors, just run theserver and go to this path /mako.
You can find a detailed explanation of how I implemented this library in my blog post named Integrating third-party templates' libraries with Django.
The MIT License (MIT) Copyright (c) 2017-2019 Ahmed Jazzar [email protected]