--- Django routing layer (URLs)

How 1.orm establish table relationships

1. Primary Key

The only field to form a corresponding relationship with the data in the table, can not be repeated, can not be empty

2. Foreign Key

A table is a foreign key of the primary key of another table, another table can be determined field record for maintaining data consistency, the foreign key can be repeated,

3. One to One

A data corresponds to a data relationship relationship, for example: a man is only an identity card, an ID card corresponds to only one person

4. many

A data correspondence relationship between multiple data relationships, such as: a book was published only a publishing house, a publishing house can publish several books

5.-many

A variety of data relationships correspondence relationship between a variety of data necessary to establish the third table, for example: a person can write more than one book, a book can be written more than one person

6. How to create a primary key in a foreign key in django

Primary key

You can create your own, if he did not create, the system will automatically help you create

bid = models.AutoField(primary_key=True)

Foreign key

Many:

publish_id = models.ForeignKey(to='Publish')  

Precautions:

1.to used to refer to a relationship with that table, the default association is the table's primary key field

2. Create the foreign key is generally written in the multi-party,

One to One

author_detail = models.OneToOneField(to='Author_detail')

Precautions:

1. one foreign key fields, when it is created, synchronized to the data in Table _id field is automatically added suffix

2. Write them multi-party

Many to many:

author = models.ManyToManyField(to='Author')

Precautions:

1.django will automatically help you create a third table,

2. many to many, no matter what to write in a table can be, but it is recommended to write in high-frequency queries party

3.author This dictionary is a virtual field, can not show up in the table, the role is just to tell orm, I need to create table Chapter III

Note: Use pymysql create a data table after the default format is latin, this time does not support Chinese input, encoding conversion is needed, the conversion method is as follows:

alter table `tablename` convert to charset utf8;  

2.django flowchart request lifecycle

You can see the detailed process of running django

3.urls.py routing layer

1. Configuration routing level

The browser sends requests over the url corresponding to the rear end view corresponding to a function to call the views in the view logic function

from app01 import views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    # 登录
    url(r'^test/',views.login)

1.url first parameter is a regular expression, regular expression as long as the content to be matched, can be performed behind the view function

2. If there is a match to the top, but in fact it should be matched to the bottom, so there will be chaos matching problem

2. The regular expression to use some basic

# ^     以.....开头
# $     以.....结尾

4. route matches

Routing match means that when the browser sends a request to the server, IP will send back url come together, conduct regular match information in urls.py, if they can match the information on the call views.py view function, for processing.

APPEND_SLASH = True  # 该参数默认是True,加False自动取消浏览器加斜杠功能 

5. Anonymous packet

Nameless grouping simply means that no group name, a regular expression in parentheses indicates a packet, the browser sends a request to the server, the browser will / url / information is also sent to the back together, if there is, you need to use a receive position parameters.

# urls.py中    url(r'^index/(\d+)/', views.index),  

# views:      def index(request,args)

6. famous grouping

Refers to the well-known packet using regular key value is matched to the line of view into the transfer function, it is necessary to use a key to be received

# urls.py中    url(r'^index/(?P<year>\d+)/', views.index),  

# views:      def index(request,kargs)

Note: also known as packets and unknown packets not mixed together, may be a plurality of single continuous use.

7. reverse lookup

In order to reverse analysis is used when the number of multi-use path, and if you change the path urls.py the path before you write in the app will all fail, for I prevent this from happening, you can use reverse lookup for processing, reverse analysis to provide the equivalent of a dynamic path, regardless of the path urls.py variations, html, views.py the path will change with urls.py in the path of change.

1. How to use

Fixed path

# 在urls.py中,设置反向解析
url(r'^login/$', views.login,name='login_page'), # 路径login/的别名为login_page

# 在views.py中,反向解析的使用:
 url = reverse('login_page')

# 在模版login.html文件中,反向解析的使用
 {% url 'login_page' %}

Unknown group

# 针对无名分组,比如我们要反向解析出:/aritcle/1/ 这种路径,写法如下
# 在urls.py中设置反向解析
 url(r'^aritcle/(\d+)/$',views.article,name='article_page'), # 无名分组

# 在views.py中,反向解析的使用:
 url = reverse('article_page',args=(1,))
    
# 在模版login.html文件中,反向解析的使用
 {% url 'article_page' 1 %}

Famous grouping

# 针对有名分组,比如我们要反向解析出:/user/1/ 这种路径,写法如下
# 在urls.py中设置反向解析
 url(r'^user/(?P<uid>\d+)/$',views.article,name='user_page'), 
    
# 在views.py中,反向解析的使用
 url = reverse('user_page',kwargs={'uid':1})
    
# 在模版login.html文件中,反向解析的使用
 {% url 'user_page' uid=1 %}

8. route distribution

Route distribution is to use multiple app integrally connected together, to solve too many routes matching the total relationship of the project, but a problem is that if two people of the same view function to write, so when conducting path match matching error appears, will be full for this from happening, you need to use the route distribution

2. Use

from app01 import urls as app01_urls
from app02 import urls as app02_urls

url(r'^app01/',include('app01.urls')),
url(r'^app02/',include('app02.urls'))

inclued distribution function is a function to do the operation, when receiving the path, it will use regular expressions to find in each routing table.

9. Name space

Because name = '' itself does not generate the name space, so in order to find will find, when it is no longer found when looking down, and when this occurs will find matching errors,

Solution:

# 1.不使用重名的别名

# 2.
url(r'^app01/',include('app01.urls')),
url(r'^app02/',include('app02.urls'))

10. The pseudo-static

A dynamic Web page disguised as a static web page, a search engine to improve SEO search query frequency and intensity

11. Virtual Environment

What is a virtual environment: a new virtual environment is equivalent to a new python interpreter

The purpose of the virtual environment: when writing app, the python interpreter will all libraries are loaded, a waste of resources, the use of virtual environments, download only the modules they need, at startup will only need to start module.

Precautions: Excessive virtual environment will result in a waste of resources occupied by the system

12.django version differences

django 1.x: urls.py in using url

django 2.x: urls.py path member is used

Matters form file transfer form to note:

1.mothod must be changed post

2.enctype into formdata format

In the early post using ultra backend sends a request when the need to go to settings configuration file to comment out a middleware crf.

# 针对于文件的操作使用的是
file_obj = request.FILES         # django会将文件数据放在request.FILES中
file_obj.name 

with open(file_obj.name,'wb') as f:
    for line in file_obj:
        f.write(line)

Guess you like

Origin www.cnblogs.com/whkzm/p/11933063.html