Getting Started with Django (iii) front-end Template language

Source: Internet
Author: User

We have now been able to decorate our HTML pages by introducing external styles, as explained in the previous two sections. But no matter how we decorate it, the published HTML is just a static page. For example, we now need each page refresh time to show on the page. The content of the static page is written dead, and now we try to use dynamic language to realize our function.


The implementation of dynamic pages in Django needs to be handled from two locations:

1. Application Folder App01 views.py to modify the content definition of the index () function to be passed to the HTML page variable content

2, in the HTML page code to add the Jinja format Dynamic scripting language, the index () function in the variable content into the HTML page (Django implementation of this function is the utility of JINJA2 this package)


Let's briefly introduce the dynamic language syntax of JINJA2. What is commonly used is to implement the For loop and if else judgment. In Python if you want to traverse a list with a for method, the code is as follows:

Li=[1,2,3]for I in Li:print i

If you implement this loop list with Jinja, the syntax becomes

{% for I in li%} 

Because HTML does not have the print syntax, the output is added with a

If the Python method will not write, directly write Jinja syntax

{% if x = = 1%} 

From the above syntax can be seen, in fact, jinja sentence format Python is very similar. The identifier was added on both sides of the judgment statement with the {%%}. Also at the end of the last time to write the closing character {% ENDXX%}. The variable is wrapped with double curly braces {{i}}. But either the list li or the variable x is passed in by index () in views.py. Let's write a complete code.

Modify index () in the views.py file first

views.py

From django.shortcuts import renderimport time# Create your views Here.def index (Request): # Assigns a value to the current time obtained after formatting to Now_time Now_time = Time.strftime ('%y-%m-%d%h:%m:%s ') # Create a dictionary that will be passed to the HTML variable as the key value, and the contents of the variable as value values Index_dic = {' Li ': [ ' Haha ', ' hehe ', ' Heihei '], ' x ': 2, ' Now ': now_time} # import the dictionary into the Render method return render (Reque St, ' index.html ', index_dic)

index.html file under Modify templates directory

Index.html

<! Doctype html>

Finally see how the page works

650) this.width=650; "src=" http://s1.51cto.com/wyfs02/M01/7D/10/wKioL1bfrkLQ86tvAAEBtIYGxlY682.jpg "title=" 1.jpg " alt= "Wkiol1bfrklq86tvaaebtiygxly682.jpg"/>


Here, we can combine python to present our desired content to the user through the front-end scripting language.




This article from "Thunderbolt Tofu" blog, declined reprint!

Getting Started with Django (iii) front-end Template language

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.