Go to: Use the Mako template in Django

Source: Internet
Author: User
Use the Mako template type: technology-related-posted at: 2010/07/27 23: 53 tags: Python Django Mako in django1.2

After reading the Mako document, I thought about changing the template to Mako. By the way, I am familiar with the syntax of Mako.

In django1.2 officially added support for third-party templates (see: http://docs.djangoproject.com/en/dev/ref/templates/api/#using-an-alternative-template-language), pondering a while later began to configure Mako under Django.

Modify the _ init _. py file under the project and overwrite the template class under Mako. template.

?
12345678910111213 #coding: utf-8 from mako import template   class DMTemplate(template.Template):           def render(self, context):         # flatten the Django Context into a single dictionary.         context_dict = {}         for d in context.dicts:             context_dict.update(d)         return super(DMTemplate, self).render(**context_dict)       setattr(template, 'Template', DMTemplate)

Create a file django_mako.py in the project path.

?
12345678910111213141516171819202122 #coding: utf-8 from mako.lookup import TemplateLookup   from django.template.loaders import app_directories   import settings   mylookup = TemplateLookup(     directories = settings.TEMPLATE_DIRS,     module_directory = '/tmp/mako_modules',     input_encoding = 'utf-8',     output_encoding = 'utf-8',     encoding_errors = 'replace',     format_exceptions = True, )       class Loader(app_directories.Loader):     is_usable = True      def load_template(self, template_name, template_dirs=None):         _template = mylookup.get_template(template_name)         return _template, _template.source

Then modify settings. py and use the defined loader.

?
12345 TEMPLATE_LOADERS = ( #    'django.template.loaders.filesystem.load_template_source', #    'django.template.loaders.app_directories.load_template_source',     'myblog.django_mako.Loader', )

In this way, you can use the Mako Template under django1.2.

Mako template engine installation and integration in Django 22:16:44 by sand, 630 visits, tags:
Mako,
Python,
Django, operating system: Linux cent OS 5/max OS X 10.6 snow leopard
Related Environment: Python 2.6.4; Django 1.1.1 installation version: Mako 0.2.5; Django-Mako 0.1.3 Mako is an open-source template engine developed in Python. It has powerful functions and is easy to use. The following describes the installation steps:
  1. Download: Go to the official website
     
  2. Decompress: Tar zxvf Mako -*
     
  3. Go to the file directory and run the following command:
    Sudo Python setup. py install
     
  4. After installation is complete, go to the site-packages directory under your python installation directory and check whether the Mako-0.2.5-py2.6.egg file exists. If yes, the installation is successful.

  5. Test Program helloword
    1) Run python to enter the python Runtime Environment

    2) enter the following Python code for testing.

    from mako.template import Template
    mytemplate = Template("hello, ${name}!")
    print mytemplate.render(name="sand")

    3) If you see hello, sand on the screen! The output indicates that the installation is successful.

Django integration Mako :(Django-Mako pluginMethod)

To use Mako in Django, you must install a separate module Django-Mako.

  1. Download: Go to the official website
     
    Or easy_install Django-Mako
     
  2. Usage:
    1) Add a djangomako. Middleware. makomiddleware example to middleware_classes in settings. py of your Django project:
    MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'djangomako.middleware.MakoMiddleware',
    )

    2) Add the Django method, for example:

    from djangomako.shortcuts import render_to_response
    def hello_view(request):
    return render_to_response('hello.html', {'name':'sand'})

    3) map the URL request/Hello to the method added above in Django

    The content of the 42.16.html template file hello.html is as follows:
    Hello $ {name }!

    5) Start your Django project and access http: // yourhostname/hello in the browser to see if the returned Hello sand is displayed!

Related Article

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.