Django Learning Note 4 templates

Source: Internet
Author: User

1. The design of the page and the Python code separation meeting is more clean, concise and easier to maintain. We can use the Django template system to implement this pattern,

A template is a text that separates the representation and content of a document. The template defines the placeholder and various basic logic (template tags) that are used to standardize how the document should be displayed.

2. Templates are often used to generate HTML, but Django templates can also produce any document based on text format. If Android can??? xml

<!DOCTYPE HTML><HTML><Head>    <title>Ordering Notice</title></Head><Body><H1>Ordering Notice</H1><P>Dear {{Person_name}},</P><P>Thanks for placing a order from {{company}}. It's scheduled to ship on {{ship_date|date: "F J,y"}}.</P><P>Here is the items you ' ve ordered:</P><U1>{% for item in item_list%}<Li>{{Item}}</Li></U1>{% if ordered_warranty%}<P>You do ' t order a warranty,so you ' re on your own when the products inevitably stop working.</P>
{% Else%}
{% endif%}<P>Sincerely,<BR/>{{company}}</P></Body></HTML>

Two curly braces {{Person_name}} are called text enclosed in variables, curly braces, and percentages, {% for item in item_list%} is a template label (Templates taG), the label is defined more than More specifically, only notify the template system to do some work, for the label is similar to the for statement

3.

The most basic way to use a Django template in Python code is as follows:
1. You can create a template object with the original templates code string, which Django also supports by specifying the template file path
Building Template object;
2. Call the Render method of the template object and pass in a set of variable context. It returns a template-based presentation string that is in the template
Variables and tags will be replaced by the context value

 fromDjango.httpImportHttpResponse fromDjango.templateImportTemplate,context#Create your views here.ImportdatetimedefHello (Request):returnHttpResponse ("Hello World")defCurrent_datetime (Request): now=Datetime.datetime.now () T=template ("") HTML=t.render (Context ({'current_date': now})) returnHttpResponse (HTML)

Django Learning Note 4 templates

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.