Eight Django framework, template language

Source: Internet
Author: User

Template language is the language in which dynamic data can be rendered in HTML templates

First, receive value rendering

The locals () function, written in the request-response render () function, can be used to upload the variables in the logic handler to HTML rendering in template language

{{...}} Receive a variable or value for rendering

Logical processing

From django.shortcuts import renderfrom app1.models import *    #导入数据库操作模块 # logic processing Module DEF special (request):    a = ' Hawking the Recording network '    return render (Request, ' index.html ', Locals ())  # Open Page

Html

<! DOCTYPE html>

Second, receive the value, run the code block

For loop

{% for item in item_list%}

<a>{{Item}}</a>

Forloop.counter counter, record number of cycles
Forloop.first determines whether the loop is the first time, returns a Boolean value
Forloop.last Determines whether the loop is the last time, returns a Boolean value

{% ENDFOR%}

Logical processing

From django.shortcuts import renderfrom app1.models import *    #导入数据库操作模块 # logic processing Module DEF special (request):    a = [' Home ', ' Product show ', ' About us ']    return render (Request, ' index.html ', Locals ())  # Open Page

Html

<! DOCTYPE html>

If judgment

{% if Ordered_warranty%}

{% Else%}

{% ENDIF%}

Logical processing

From django.shortcuts import renderfrom app1.models import *    #导入数据库操作模块 # logic processing Module DEF special (request):    a = [' Home ', ' Product show ', ' About us ']    return render (Request, ' index.html ', Locals ())  # Open Page

Html

<! DOCTYPE html>

Three, motherboard-sub-board-motherboard inheritance

The parent board inherits the page that is visited inherits a motherboard, introduces the content of the access page to the location specified in the motherboard, and combines it into a new page to return to the browser

The general motherboard is written in a page where the unchanging, that is, the common place, the inherited page (Access page) is each page different place, that is, the page is different from the introduction of the master board into a new page back to the browser

The motherboard is usually a Web page, head, bottom, head bottom css, head bottom JS

Inherited pages (access pages) are generally new content, CSS and JS for new content

Motherboard: {% block title%} {% Endblock%} Set callable chunk or, parameter set callable chunk, no parameter call block
Sub-board: {% extends "base.html"%} Child Board inheritance motherboard
{% block title%} {% Endblock%} Set callable chunk or, parameter set callable chunk, no parameter call block

Logical processing

From django.shortcuts import renderfrom app1.models import *    #导入数据库操作模块 # logic processing Module DEF special (request):    return Render (Request, ' index.html ', Locals ())  # Open Page

Master Board

<! DOCTYPE html>

Child Board (Access page)

{% extends "tou.html"%}  #继承母板 {% block css%} #设置母版调用css样式区块    <link rel= "stylesheet" type= "Text/css" href= "/static/css/index.css" >{ % endblock%}{% block Nei_rong%} #设置母版调用字板内容区块    <div id= "Neir" > Content </div>{% endblock%}

Iv. Methods of Help:
{{item.event_start|date: ' y-m-d h:i:s '}} format time
{{bio|truncatewords: ' 30 '}} displays only the specified number of words
{{My_list|first|upper}} Capitalize first letter
{{Name|lower}} first Letter lowercase

Five, custom HTML template can call function module

A. Create a templatetags directory in your app

B. Create any. py file in the directory, such as: xx.py

Set the HTML callable function in the py file, note that there are a few points to note in the Py file:

#!/usr/bin/env python# coding:utf-8from Django Import template #必须导入这个模块from django.utils.safestring import m Ark_safe      #必须导入这个模块register = template. Library ()     #固定格式必须设置这个变量 @register. Simple_tag                  #自定义函数必须用上这个装饰器, the HTML page can call Def my_simple_time (v1, V2, v3):  #自定义函数    Return v1 + v2 + v3@register.simple_tagdef my_input (ID, arg):    result = "<input type= ' text ' id= '%s ' class= '%s '/> "% (ID, ARG,)    return Mark_safe (Result)

C. Import a custom function module in an HTML file that uses a custom function xx.py file name
{% load Custom Function module name%}

d, HTML page use
{% function name parameter parameter%}

{% load Mbanyuyan%}  #导入自定义函数模块 <! DOCTYPE html>

Note: With custom HTML functions, the app must be registered with Installed_apps in the global profile settings.py

Installed_apps = (    ' django.contrib.admin ',    ' Django.contrib.auth ',    ' django.contrib.contenttypes ',    ' django.contrib.sessions ',    ' django.contrib.messages ',    ' django.contrib.staticfiles ',    ' App01 ',)

Eight Django framework, 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.