加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Django之url映射

发布时间:2020-12-15 17:18:38 所属栏目:大数据 来源:网络整理
导读:div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?opt=1"gt; h1 id="url-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;"url

<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?opt=1"&gt;
<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),]

 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)

,可以在匹配 url的同时传递参数,而且匹配的范围更加庞大。例如:

/year/',views.year)

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">int</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">匹配0和正整数</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: white; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">slug</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">匹配字母、数字、横杠、下划线组成的字符串,str的子集</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: #f8f8f8; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">uuid</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">匹配格式化的UUID,如075194d3-6885-417e-a8a8-6c931e272f00</td>

</tr>
<tr style="border-width: 1px 0px 0px; border-right-style: initial; border-bottom-style: initial; border-left-style: initial; border-right-color: initial; border-bottom-color: initial; border-left-color: initial; border-image: initial; border-top-style: solid; border-top-color: #cccccc; background-color: white; margin: 0px; padding: 0px;">
<td style="text-align: center; font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">path</td>
<td style="font-size: 1em; border: 1px solid #cccccc; margin: 0px; padding: 0.5em 1em;">匹配任何非空字符串,包括路径分隔符,是全集</td>

</tr>

:,:})

<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参数和字典类型参数都使用了,并且名称相同,如:

:})

)

 = =>

,views.day)

,views.day)

d+)/day/",views.day)

))

 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,再进行相应的视图函数映射。


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!