03.django upgrade Daguai remember learning

00. Templates

In the Django framework, templates can help developers to quickly generate presented to the tool user page.

Template processing:

a. Load

b. Rendering

Template has two major components

a.HTML static code

b. snippet dynamically inserted (digging, filled pit)

 

01. Syntax

a. dot syntax template: grades grade

Properties or methods: grade.gname

Index: grades.0.gname

 

b. template tags: {% tag%} Syntax

Role: b1 incoming load external variables.

b2. Create a text in the output

b3. logic or control loop

cif statement

Expression {%}%

Statement

%} {% Elif

Statement

{% else %}

Statement

{% endif% }

 

d.for statement

Variable in the list {% for%}

Statement

{% empty %}

Statement

{% endfor %}

 

 

 

 

e. Comment

Single-line comments

 

 

Multi-line comments

 

 

 

. f filter {{var | filter}}

Role: modified before variable display

add {{p.page | add:5}}

No subtraction filters, but where you can add a negative wig

{{p.page | add: -5}}

lower

{{p.pname|lower}}

join: pass parameters

{{students|join '='}}

{{var|defualt value}}

dateVal: Specifies the date string format conversion, the processing time

{{dateVal | data:'y-m-d'}}

g.html Escape: The received data is treated as an ordinary character string as to render the HTML code of a problem

html:{{code|safe}}

{%}% Off autoescape

code

%}% {Endautoesape

 

{%}% Is autoescape

code

%}% {Endautoesape

 

 

05. View Summary

Structure tags

*block

Piece

We used to plan the layout (digging)

For the first time, representatives of planning

The second time, on behalf of filling the previous plan

 

 

 

The third time, on behalf of filling the previous plan, the default action style cover

* If you do not cover, may be added {{block.super}}

* Achieved an incremental operation

super: representatives inherit the parent class or superclass

 

 

 

*extends

inherit

You can get all the structural character templates clock

 

 

 

 

*block+entends

Dismembered

*include

*contain

* As part of the page may be embedded into other pages

 

 

 

 

* Include + block (by the poly-zero)

* You can also mix the three labels, try not to use include

* If we inherit from a character template, directly from their own templates page rewrite the formula does not take effect, and can only be filled in the existing pit.

 

 

 

 

urls

* Routers

- to match the writing order list

- from top to bottom match, there is no optimal matching of concepts

* Write routing rules

- We usually directly added directly at the end of a backslash

- Parameters

Path parameter (position parameter - to match the writing order)

Keyword arguments (in accordance with the parameter name matching, and is unrelated to the order)

The number of parameters must be consistent with the number of parameters view function (other than the default request)

 

* Parameters using the router () were acquired

- a view function corresponds to a parenthesis in

 

 

 

 

 

 

 

 

 

06. view

Python is a view of the nature of the function

In response to the view into two categories:

Returns the data format JSON

The situation returned to the page: redirect to another page / wrong view (40x, 50x)

View response process: Browser Input -> django Ip port and remove access to information,

The remaining path -> route matches the URL -> response View -> back to the browser

 

07.URL reverse lookup

url reverse lookup

In the root urls:

path('view/',include('view.learn', namespace='view'))

在子urls中

path('hello/', views.hello, name=‘sayhello’)

使用反向解析优点:如果在视图,模板中使用硬编码连接,在url配置发生改变时,需要变更代码非常多,这样导致我们的代码结构不是很容易维护,使用反向解析可以提高我们代码的扩展性和课维护性。

 

解决问题: https://docs.djangoproject.com/en/3.0/topics/http/urls/

Reversing namespaced URLs

反向解析:

*根据路由中注册的namespace和在子路由中注册name,这两个参数来动态获取我们的路径

*在模板中使用{% url`namesspace'}

 

 

 

错误页面定制

*在错误中重写对应错误状态码的页面

*关闭Debug

*实现原则(就近原则)

 

 

07.HttpRequest

 

 

 

-------------------------------------------

双R

*Request

内置属性(method/path/GET/POST/META)

*Response

 

 

备注:

静态资源:

*动静分离

*创建静态文件夹

*在setting中注册STATICFILES_DIRS=[]

*在模板中使用

——先加载静态资源(%load static %)

——使用{% static xxx %} xxx相对路径

*坑点

——仅在debug模式可以使用

——以后需要自己单独处理

 

备注:pycharm手动修改py文件才会重载,修改html不会重载

Guess you like

Origin www.cnblogs.com/aixiaoxiaoyu/p/12178031.html