Django Default template is too inflexible, want to put a number 0.15 to show 15% is a lot of effort, too uncomfortable!!!
Online access to a few template system, there are Jinja2 and so on, and finally found that Mako can directly support the Python statement, the most flexible, decisive choice.
The use of the process encountered a lot of problems, recorded as follows:
1. Debugging skills: If there is a problem in the Mako template display process, Django error prompt is a word, "wrong", the rest, slowly check it, and then I delete each, try, a miserable word. A few mistakes broke down. Finally, the solution is found on the StackOverflow, the code is as follows:
From Mako.lookup import Templatelookup
From Mako Import exceptions
Risk_lookup = Templatelookup (
Directories=risk_platform.settings.template_dirs,
Input_encoding= ' Utf-8 ',
Output_encoding= ' Utf-8 ',
default_filters=[' None_empty ', ' H ',],
imports=[' from risk.views import None_empty '],
)
def render_to_response (filename, CTX):
Try
TP = risk_lookup.get_template (filename)
Cont = Tp.render (**CTX)
Return HTTP. HttpResponse (cont)
Except:
Return HTTP. HttpResponse (Exceptions.html_error_template (). Render ())
Once an exception occurs in the render process, it is output by using Mako to show the wrong way. A few days of refreshing.
2. The variables used in the For loop in Moko override variables in the context, so the following template code does not work:
% for name in names:
<td>${name}</td>
% End for
<tr>${name}</td>
The following ${name} will not be able to get the relevant variable value in the context. At present I do not have time to consult the relevant information, and so on time to look at it slowly.
3. Djangomako This package can not be used at all, a run on the wrong (my version is python3.4 django_mako-0.1.3) Look at the source code, two or three lines of useful, simply do not use his bag
Mako template use tips and debugging