Create and use a template in the Python Django framework, pythondjango

Source: Internet
Author: User

Create and use a template in the Python Django framework, pythondjango

How to use the template System

Let's take a deeper look at the template system and you will understand how it works. However, we do not intend to combine it with the previously created view, because our current goal is to understand how it works independently. . (In other words, you usually use the template and view together, but we just want to highlight that the template system is a Python library and you can use it anywhere, not just in the Django view .)

The most basic way to use a Django template in Python code is as follows:

  • You can use the original Template code string to create a Template object. Django also supports creating a Template object by specifying the Template file path;
  • Call the render method of the template object and input a set of variables context. It returns a template-based display string, and the variables and labels in the template are replaced by the context value.

The Code is as follows:

>>> from django import template>>> t = template.Template('My name is {{ name }}.')>>> c = template.Context({'name': 'Adrian'})>>> print t.render(c)My name is Adrian.>>> c = template.Context({'name': 'Fred'})>>> print t.render(c)My name is Fred.

The following is a step-by-step introduction
Create template object

The simplest way to create a Template object is to instantiate it directly. The Template class is in the django. template module. The constructor accepts a parameter and the original Template code. Let's dig deeper into the Python interpreter to see how it works.

Go to the project directory (created by the django-admin.py startproject command in chapter 2) and enter the command python manage. py shell to start the interaction interface.

A special Python prompt

If you have used Python before, you must be curious about why we run python manage. py shell instead of python. Both commands start the interactive interpreter, but the manage. py shell command has an important difference: Before starting the interpreter, it tells Django which setting file to use. Most subsystems of the Django framework, including the template system, depend on the configuration file. If Django does not know which configuration file to use, these systems will not work.

If you want to know, I will explain how it works. Django searches for the DJANGO_SETTINGS_MODULE environment variable, which is set in settings. py. For example, if mysite is in your Python search path, DJANGO_SETTINGS_MODULE should be set to 'mysite. setting '.

When you run the command: python manage. py shell, it will automatically Help You With DJANGO_SETTINGS_MODULE. In the current examples, we encourage you to use ''python manage. py shell '', which saves you the trouble of configuring environment variables you are not familiar.

As you get familiar with Django, you may prefer to discard using ''manage. py shell''. Instead, manually add the environment variable DJANGO_SETTINGS_MODULE to your configuration file. bash_profile.

Let's take a look at some basic knowledge about the template system:

>>> from django.template import Template>>> t = Template('My name is {{ name }}.')>>> print t

If you work with us, you will see the following content:

<django.template.Template object at 0xb7d5f24c>

0xb7d5f24c is different each time. It does not matter. This is only the ID of the Template object during Python runtime.

When you create a Template object, the Template system internally compiles the Template to the internal format, optimizes it, and prepares for rendering. If your Template syntax is incorrect, A TemplateSyntaxError exception will be thrown when Template () is called:

>>> from django.template import Template>>> t = Template('{% notatag %}')Traceback (most recent call last): File "<stdin>", line 1, in ? ...django.template.TemplateSyntaxError: Invalid block tag: 'notatag'

Here, the block tag points to ''{% notatag %}'', which is synonymous with the template tag.

The system will throw a TemplateSyntaxError exception in the following circumstances:

  • Invalid tags
  • The tag parameter is invalid.
  • Invalid Filter
  • The filter parameter is invalid.
  • Invalid template syntax
  • Unclosed block tags (for block tags to be closed)


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.