您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Django 14天从小白到进阶- Day2 搞定url组件

5b51 2022/1/14 8:24:07 python 字数 13172 阅读 645 来源 www.jb51.cc/python

本节内容 路由系统 models模型 admin views视图 template模板 路由系统 我们已知,用户从浏览器发出的请求会首先打到django url的路由分发系统这里,然后再到views视

概述

本节内容

我们已知,用户从浏览器发出的请求会首先打到django url的路由分发系统这里,然后再到views视图--》models模型--》template模板--》用户浏览器。

换言之,urls.py 文件主载着你整个网站的所有页面&接口的url分配。 

正式开讲django的路由语法前,先要知道路由分为以下两种:

静态路由:已经明确定义好的一条路由,比如下例,用户只能在浏览器上输入/articles/2003/ 才能匹配到这条路由,输入任何其它的都匹配不上本条。

动态路由:定义的只是路由规则,比如只能输入数字、或特定排列、长度的字符等,你不知道用户会具体输入什么,只要符合你的规则即可。比如通过博客园每发篇文章,就会为这篇文章产生一个新的url,这个url肯定不可能是后台程序员手动给你填加的,那他得累死。肯定是他写好规则,比如8662706就代表这个文章编号,这个编号可能是数据库中此文章的id,这个不管,程序员在定义路由时,只需规定,后面用户输入的url必须是数字就行。

Django 的路由本质上是通过正则表达式来对用户请求的url进行匹配 

from app01 import views

urlpatterns = [
re_path(r'articles/2003/$',# 静态路由
re_path(r'^articles/(?P [0-9]{4})/$',views.year_archive),# 动态路由
re_path(r'^articles/(?P [0-9]{4})/(?P [0-9]{2})/$',views.month_archive),# 动态路由
re_path(r'^articles/(?P [0-9]{4})/(?P [0-9]{2})/(?P [\w-]+)/$',views.article_detail),# 动态路由
]  

urlpatterns = [
re_path(r'articles/2003/$',# 静态路由
re_path(r'^articles/(?P [0-9]{4})/$',views.year_archive),# 动态路由
re_path(r'^articles/(?P [0-9]{4})/(?P [0-9]{2})/$',views.month_archive),# 动态路由
re_path(r'^articles/(?P [0-9]{4})/(?P [0-9]{2})/(?P [\w-]+)/$',views.article_detail),# 动态路由
]  

urlpatterns = [
re_path(r'articles/2003/$',# 静态路由
re_path(r'^articles/(?P [0-9]{4})/$',views.year_archive),# 动态路由
re_path(r'^articles/(?P [0-9]{4})/(?P [0-9]{2})/$',views.month_archive),# 动态路由
re_path(r'^articles/(?P [0-9]{4})/(?P [0-9]{2})/(?P [\w-]+)/$',views.article_detail),# 动态路由
]  

 

以上路由对应views.py视图方法

return HttpResponse("dddd")

def year_archive(request,year):
return HttpResponse("year_archive" + str(year))

def month_archive(request,year,month):
return HttpResponse("month_archive %s-%s" %(year,month))

def article_detail(request,month,slug):
return HttpResponse("article_detail %s-%s %s" %(year,slug))

 

虽然实现了路由匹配,但url中的代码看着很笨拙+丑陋,不过在黑暗的django 1.0时代,大家只能这么玩。不过解放之神django2.0来啦,带来了路由匹配新玩法。

from . import views

urlpatterns = [
path('articles/2003/',path('articles/ /',path('articles/ / /',path('articles/ / / /',]

urlpatterns = [
path('articles/2003/',path('articles/ /',path('articles/ / /',path('articles/ / / /',]

urlpatterns = [
path('articles/2003/',path('articles/ /',path('articles/ / /',path('articles/ / / /',]

先说,实现的功能跟之前用re写的路由一样,但是直观来看,是不是干净了很多?

下面解释语法,我太懒,直接把官网解释copy来啦,

Notes:

Example requests:

The following path converters are available by default:

上面这些自带的converter已经可以满足你大部分的url匹配需求,但有特殊情况不能满足时,你还可以自定义converter哈。

A converter is a class that includes the following:

For example:

def to_python(self,value):
    return int(value)

def to_url(self,value):
    return '%04d' % value</pre>

  

通过上面的regex参数来看, 其实django 2.0 这个path converter本质上也只是通过对正则表达式进行了封装,使其调用更简单而已。

Register custom converter classes in your URLconf using :

from . import converters,views

register_converter(converters.FourDigitYearConverter,'yyyy')

urlpatterns = [
path('articles/2003/',path('articles/ /',...
]

register_converter(converters.FourDigitYearConverter,'yyyy')

urlpatterns = [
path('articles/2003/',path('articles/ /',...
]

register_converter(converters.FourDigitYearConverter,'yyyy')

urlpatterns = [
path('articles/2003/',path('articles/ /',...
]

  

urlpatterns = [

... snip ...

path('community/',include('aggregator.urls')),path('contact/',include('contact.urls')),# ... snip ...

]

path('community/',include('aggregator.urls')),path('contact/',include('contact.urls')),# ... snip ...

]

path('community/',include('aggregator.urls')),path('contact/',include('contact.urls')),# ... snip ...

]

  

django 在匹配url时,只要遇到include()语法, 就会把url分成2部分,比如上面代码里的url,只要匹配上community/,就会把整条url丢给include('aggregator.urls')子urls.py。 子urls.py负责匹配后面的部分。

如果url 中出向很多重复的部分,可以按下面的方法聚合

extra_patterns =<span style="color: #000000;"> [
path(<span style="color: #800000;">'<span style="color: #800000;">reports/<span style="color: #800000;">'<span style="color: #000000;">,credit_views.report),path(<span style="color: #800000;">'<span style="color: #800000;">reports/ /<span style="color: #800000;">'<span style="color: #000000;">,path(<span style="color: #800000;">'<span style="color: #800000;">charge/<span style="color: #800000;">'<span style="color: #000000;">,credit_views.charge),]

urlpatterns =<span style="color: #000000;"> [
path(<span style="color: #800000;">''<span style="color: #000000;">,main_views.homepage),path(<span style="color: #800000;">'<span style="color: #800000;">help/<span style="color: #800000;">',include(<span style="color: #800000;">'<span style="color: #800000;">apps.help.urls<span style="color: #800000;">'<span style="color: #000000;">)),path(<span style="color: #800000;">'<span style="color: #800000;">credit/<span style="color: #800000;">'<span style="color: #000000;">,include(extra_patterns)),]

in this example,the  URL will be handled by the  Django view.

URLconfs have a hook that lets you pass extra arguments to your view functions,as a Python dictionary.

The  function can take an optional third argument which should be a dictionary of extra keyword arguments to pass to the view function.

For example:

urlpatterns = [
path('blog/ /',views.year_archive,{'foo': 'bar'}),]

In this example,for a request to ,Django will call .

*注:这个功能很少用,知道就行了。 

到此,我们就掌握了django url系统的大部分用法,是不是很简单?

Now,it's time to move forward。

  

总结

以上是编程之家为你收集整理的Django 14天从小白到进阶- Day2 搞定url组件全部内容,希望文章能够帮你解决Django 14天从小白到进阶- Day2 搞定url组件所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶