Mako template use tips and debugging

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.