<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?opt=1">
<h1 id="url-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">url映射的作用
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">根据Django的MTV模式,url的映射是根据用户输入或传送而来的url路径,来进行区分去执行相应的view函数来响应用户的操作。
<h1 id="url-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">url映射的方式
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">Django项目的创建后,会自动创建和你项目名称相同的全局文件包,urls.py就在其中。
,admin.site.urls),]
自动添加的一条映射。我们的view函数在自己创建的app中,所以需要先引入app文件包中的views
blog views
添加自己的映射条件,例如
blog views
urlpatterns = [
path(,path(,views.blog)
]
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">写相应的视图函数
django.shortcuts render,HttpResponse
<span class="hljs-function" style="color: #7aa6da;"><span class="hljs-keyword" style="color: #c397d8;">def
<span class="hljs-title" style="color: #969896;">blog<span class="hljs-params" style="color: #e78c45;">(request):
<span class="hljs-keyword" style="color: #c397d8;">return HttpResponse(<span class="hljs-string" style="color: #b9ca4a;">"WELCOME")
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">这样在浏览器中输入127.0.0.1:8000/blog/就能看到返回的WELCOME了。
<h1 id="url-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">url映射的函数
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">urlpatterns中可以使用两种函数,path()用来字符串路由,re_path()处理正则式路由。
<h3 id="path-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 20px; border-bottom: 1px solid #aaaaaa;">path函数
<h5 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 16px; border-bottom: 0.5px solid #aaaaaa;">必须有的参数
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">path()中有两个必须填的参数,一个是路径字符串,一个是所调用的函数。就如上边的例子
,views.blog)
加上blog/的情况,然后执行views下的blog函数,表示静态,精确的映射。路径字符串除了填精确的路径外,还可以填<类型:变量名>,可以在匹配 url的同时传递参数,而且匹配的范围更加庞大。例如:
/year/',views.year)
函数。这里注意:views.year函数的参数名必须和你在url映射时起的名字相同,即“year”。这里的数据类型还有其他几种:
函数传递的参数,一个是别名。用来给视图函数传递的参数除了上述直接在url中,还可以手动指定填入path()中,以字典的形式填入,因为这里的参数类型为**kwargs。而视图函数中的参数同样必须和字典中的键名相同。例如:
:,:})
<span class="hljs-function" style="color: #7aa6da;"><span class="hljs-keyword" style="color: #c397d8;">def
<span class="hljs-title" style="color: #969896;">year<span class="hljs-params" style="color: #e78c45;">(req,year,name,age):
str1=str(year)+<span class="hljs-string" style="color: #b9ca4a;">" "+name+<span class="hljs-string" style="color: #b9ca4a;">" "+str(age)
<span class="hljs-keyword" style="color: #c397d8;">return HttpResponse(str1)
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">如果url参数和字典类型参数都使用了,并且名称相同,如:
:})
函数中只能添加一个year参数,并且在字典中的year的数据20会覆盖掉url中的数据。最后一个参数是别名,例如:
)
= =>
函数
函数和path()函数类似,但它是使用正则表达式的模式来代替字符串模式进行匹配,只要符合正则式的模式就可以匹配成功。
,views.day)
,views.day)
自动当参数传递给视图函数,但是视图函数的形参必须要多写一个参数,名字任意。
\d+)/day/",views.day)
函数必须以其名字作为参数名,同样,如果手动添加的参数与其名称相同,那么手动添加的参数数据会将正则式中的覆盖。
错误,产生混淆,那么我们可以在每个app下建立自己的urls.py来处理自己的映射关系,在全局urls下进行分发。具体方式为:在全局urls中添加
))
django.urls path,re_path
blog views
urlpatterns = [
path(<span class="hljs-string" style="color: #b9ca4a;">"login/"
,views.login)
]
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">那么在地址栏中输入/blog/login,就会先由全局urls分发到blog下的urls,再进行相应的视图函数映射。