Day47 Django basics, routing configuration, space name

1. The easiest route configuration

URL configuration (URLconf) as the Django directory site support. Its essence is a mapping table between the URL and the URL you want to call that view function; you're in such a way to tell Django, the client sent to a URL which calls the corresponding period of logic code execution.

 1.1

Example 1:

 

Step 1:

urls.py written

urlpatterns = [
    path('admin/',  admin.site.urls),
    path('articles/', views.articles),
]

 

Write request is returned to the response object in the views.py

DEF Articles (Request):
     Print ( " article pages " )
     return HttpResponse ( " article pages " )

 

 Summary: The above is the most basic of this, without url back without any parameters.

1.2 path method to write the url dead

urls.py written

path("articles/2009/", views.articles),

 

Access becomes:

The original articles / no use to change the routing information.

 

 1.3re_path

# In order to solve the above url-coded path, with re_path

Objective: To reduce code redundancy, to write with a regular

# Articles accepts a parameter required
# urls.py file written in, which accepts a parameter function requires artcles
re_path ( "^ articles / ([ 0-9] {4}) / $", views.articles1), # unknown packet
 summary; the need to write a function parameter in views, to accept
([0-9] {4}) / $ parameter matches out
 Then add a parameter # of views.
Articles DEF (Request, the y-): 
Print ( "article pages")
return HttpResponse ( "article pages")
 Results are as follows: 

Summary: url plurality of input can be achieved in this way, to meet regular.
# Articles requires two acceptable parameter
codes # urls of:
re_path (R & lt "articles ^ / ([0-9] {}. 4) / ([0-9] {2}) / $", views.articles), # unknown grouped by location-parameters, the parameters are sequentially behind the articles the articles pass function views
# views code:
Articles DEF (Request, the y-, m): 
Print ( "article pages", the y-, m)
return HttpResponse ( "article pages")

 pycharm console print out the code:

# 2 that is acquired by the parameter / articles / behind / 2019/12 / etc.

 

Note the use of $ ## ^ and the

 1.4 URl and re_path If you want to match the regular rules require the use of ^ and $ to cards, internal path to limit the dead (must match exactly)

 1.5

1 . Path inside a good package, inclusive of automatic restriction rules
 2. url no package, you need to add the rules of regular (with re_path method) to manually 
# plus before and after the url parameters are accessible / dsdarticles / access to / articles / sdad can access the card if you want to write your own words on the need to match the regular
# forth inside path are card dead,
which is the same url and re_path

urls code:

URL ( 'Articles /', views.articles_url), 
views the code :( the above two cases are not the parameter ha)
articles_url DEF (Request): 
Print ( "article pages")
return HttpResponse ( "article pages")

 

1.6 anonymous group see above

By location parameter passing

1.7 famous group (by keyword parameter passing)

## (1) passing a parameter to a function in views / 2019

url.py Code:

re_path ( "? ^ articles / ( P <year> [0-9] {4}) / $", views.articles1), # packets known 
function of views:
a function of processing received url: articles1:
articles1 DEF (Request, year): 
Print ( "article on page 111", year)
return HttpResponse ( "article on page 1111")

## (1) processing in the client url parameter is 2, the reference parameter is the keyword transmission parameter passing.

urls.py file Code:

re_path (R & lt "Articles ^ / (? P <year> [0-9] {}. 4) / (? P <month The> [0-9] {2}) / $", views.articles), 
# stars url parameter is passed to articles in two mass participation are keywords must be written on the month and year in articles in the job function.
views the code:
Articles DEF (Request, month The, year): 
Print ( "article pages", year, month The)
return HttpResponse ( "article pages")

 1.8

To put it plainly, it is the customer, a variety of input you inside the line, accept the judgment and return. In the form of customer input / articles / parameter / line we need to deal with internally, accept the parameters (a combination of well-known points (pass argument must be key parameters defined in the url,

In the views in the corresponding function parameters) unknown packets (the order parameter by parameter passing on the line, the corresponding parameter in the function of the views, the name can easily accept the line))  

Summary: For the routing layer of 1.1-1.8

1.url and re_path almost no internal restrictions, you can add parameters or add parameters later in front of the line access, but only at a time URls add routes in a project, a bit of trouble.

2.解决1的麻烦就是在url和re_path中使用正则表达式,可以在访问/articles/.... 前面不变的情况下 增加更多的参数的  上面介绍了加1中参数和2中参数到我们视图处理函数views中,对应接受就行。

 # 上面的URl中的路由信息全部写在了主项目中url中:

 3.不足的地方:每次都需要在主项目的urls中去写上路由的配置信息,有点繁琐,不利于维护。

解决办法:在每一个app中建立url,在主项目下的url创建分组,从主文件向下app的url中找即可。

 2.路由分发

 在上图的主项目的url中写上上面的路由分发。

然后对应在app01的url写上真的路由信息:

 

然后对应在app01的url写上真的路由信息:(同下)

 在app02的URls中写上真的路由信息:(只是在主项目的文件下url中就行查找,是哪个下对应的url路由信息,不至于不像之前没有分组的时候那么乱,全部写在一起,只需要在主文件的URl写上最上面的那个信息即可)

# 类似于在广东省下面去找深圳市是一样的道路

views中的代码:

def test(request):
return HttpResponse('app02-test')

 

 3.

 

Guess you like

Origin www.cnblogs.com/longerandergou/p/11128027.html