Django Basics-Templates and routing distribution

Source: Internet
Author: User
Tags aliases

Django Templates

First what is a template?

In simple terms, a Web page can be used to respond to a user's view

The goal is to solve complex display problems.

2. Setup issues with templates

Templates configuration in the setting.py

1) Backend: Specify the template of the search engine, do not change

2) DIRS: Specify the directory where the template resides

dirs=[' index.temp ', ' music.temp ']

However, if dirs is empty, Django automatically searches the Templates folder in each app as the management directory for the template

Recommendation: Dirs remain empty, but you must create a templates folder in each app

3) App_dirs

True: First find the template from the folder specified in Dirs, and then search the templates directory if the specified template is not found

3. How the template is loaded

HttpResponse () This is the string that is loaded

1. Use loader to get the template and respond via HttpResponse

The template is converted to a string before it is finally returned to the template.

2. Using render to load directly

return render (Request, ' template name ', {parameter dictionary})

4. The name parameter of the URL () function (this usage is more flexible)

Urlpatterns = [

URL (regex,views, Kwargs =none, name = None)

]

Name: Defines the alias of the current URL, allowing aliases to be used in the template to

For example:

Urlpatterns = [

URL (regex,views, Kwargs =none, name = ' my ')

]

Use a lot of hyperlinks

Need to use tags

{% URL ' name value '%} #服务器解析

<a href= ' {%url ' my '%} ' > go to Second page </a>

Note: Name aliases cannot be defined repeatedly

URL Parameters

<a href= '/music/show/35 ' > go to the Show/35 Address page </a> #不用别名的条件下写全路由

<a href= ' {%url ' my ' %} ' > go to show/87 page </a> #传参数, add space and parameters directly after ' Alias '

Template syntax:

1. Variables

Role: Allows the back end of the data to be passed to the template (HTML), in the template, according to the actual value of the variable to display

1) data types that are allowed to be passed to a variable

Number, string, list, tuple, dictionary, function, Object (Class) (Note: Collection not possible)

Note: The function must return a value

2) How to pass variables to the template

Render

dict={

' Variable 1 ': ' Value 1 ',

' Variable 1 ': ' Value 1 ',

}

return render (Request, ' xx.html ', dict)

Focus:

If it is a list, tuples, dictionaries, etc., first organize the good data, and then put the data in Dict

For example:

Dict ={

' L ': list,

' d ':d ICT,

}

or with locals () organize local variables into dictionaries for incoming parameters, except for objects

3) Get the value of the variable in the template

{{variable name}} #字符串

{{variable name. 1}} #列表/tuple

{{variable name. Key}} #字典

{{function name}} #函数

{{class name. Method Name}} #对象

2. tags (for more label usage can look at official documents)

1) What is a label?

Allows to nest some logical operations of server segment ends into templates

And the variable just values, the label tube operation

2) syntax

{% %}

Note: Written procedure operators, variables must be separated by a space

Common tags

1. {% if condition%} #条件可以用简单逻辑判断, e.g. > < = = etc.

{% Else%}

{% End If%}

Accept not, and, or operations

But and and or cannot appear at the same time

The following is treated as false:

Empty []

Empty ()

Empty {}

Value 0

Empty string

None

{% Ifequal value 1 value 2%} #判断值1 and value 2 are equal, equivalent to value 1 = = value 2

{% Else%}

{% endifequal%}

2. For label

{% for variable in list | tuple | Dictionary%}

{% endfor%}

Allowed built-in variables (disclaimer):

Forloop

1. Forloop.counter: Record the number of cycles, starting from 1

2. Forloop.counter0: Ibid., starting from 0

3. Forloop.revcounter: Record the number of non-traversed

4. Forloop.revcounter0: Ibid., starting from 0

5. Forloop.first: Boolean value, whether the tag is the first item (usually some style settings for the first and last)

6. Forloop.first: Boolean value, whether the tag is the last item

<li {% if Forloop.first%} style= ' color:red ' {% endif%} ></li>

3. Filter

Run to filter or change the data before displaying the variable

Grammar:

{{var | filter}}

Common filters:

1. {{var| upper}}

Rewrite English letters to uppercase

2. {{var| lower}}

Rewrite the English letter to lowercase

Commonly used filters

1. Addition operation

Var|add: ' num '

First | Add:second #first and second are a list of two numeric types

2. Rounding of decimals (financial and delivery systems)

var | Floatformat:num # NUM Specifies how many decimal places are reserved

3. Extra long characters display ellipses (some strings are displayed)

Value | Truncatechars:num #num指定保留的字符数

Value | Truncatewords:num

You can also use CSS to implement

White-space:nowrap

Text-overflow:ellipsis

Django Basics-Templates and routing distribution

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.