url控制器html
from django.contrib import admin from django.urls import path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), path('index/',views.index), ]
视图python
from django.shortcuts import render # Create your views here. def index(request): import datetime now=datetime.datetime.now() ctime=now.strftime("%Y-%m-%d %X") return render(request,"index.html",{"ctime":ctime})
模板git
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h4>当前时间:{{ ctime }}</h4> </body> </html>
from django.urls import path,re_path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), # 路由配置:哪个路径由哪个视图函数处理 path('timer/', views.timer), # 带正则表达式的【路由配置】 # special_case_2003(request) re_path(r'^articles/2003/$', views.special_case_2003), # /articles/2003/ --> views.month_archive(request) 没有分组,不会传参 # 只要分组就会将分组的数据传给year_archive:year_archive(request, 分组的数据) re_path(r'^articles/([0-9]{4})/$', views.year_archive), # /articles/2005/ --> views.year_archive(request, '2005') # month_archive(request, 分组的数据1, 分组的数据2) re_path(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive), # /articles/2005/03/ --> views.month_archive(request, '2005', '03') ]
使用命名的正则表达式组来捕获URL 中的值并以关键字 参数传递给视图 r'(?P<...>)'
正则表达式
from django.urls import path,re_path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), # /articles/2005/ --> views.year_archive(request, year='2005') re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), # /articles/2005/03/ --> views.month_archive(request, year='2005', month='03') re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive), ]
URLconf中的path
能够直接将路径请求发送给对应的视图函数进行处理,也能够转发给另外一个URLconf,使用include
函数设置要转发的URLconf,子URLconf接收到的是匹配事后的余下字符串。下降耦合度django
project/urls.py/app
from django.urls import path,re_path from app01 import views urlpatterns = [ path('admin/', admin.site.urls), # 将路径请求发送给对应的视图函数 path('timer/', views.timer), # 转发给另外一个URLconf, re_path(r'^app01/', include('app01.urls')), re_path(r'^app02/', include('app02.urls')), ]
app01 /urls.py/函数
from django.urls import path,re_path from app01 import views urlpatterns = [ re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive), ]
app02 /urls.py/工具
from django.urls import path,re_path from app01 import views urlpatterns = [ re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive), re_path(r'^articles/(?P<year>[0-9]{4})/(?P<month>[0-9]{2})/$', views.month_archive), ]
访问路路径就变成了:ui
.../app01/articles/2018/
、.../app01/articles/2018/11
url
.../app02/articles/2018/
、.../app02/articles/2018/11
在须要URL 的地方,对于不一样层级,Django 提供不一样的工具用于URL 反查:
在模板中:使用url 模板标签。
在Python 代码中:使用from django.urls import reverse()函数
app01/urls.py: re_path(r'xx', views.xx, name='xxx')
from django.conf.urls import url from . import views urlpatterns = [ #... re_path(r'^articles/([0-9]{4})/$', views.year_archive, name='news-year-archive'), #... ]
在模板中: {% url 'xxx' xxxx %}
<a href="{% url 'news-year-archive' 2012 %}">2012 Archive</a> <ul> {% for yearvar in year_list %} <li><a href="{% url 'news-year-archive' yearvar %}">{{ yearvar }} Archive</a></li> {% endfor %} </ul>
在views中: reverse('xxx', args=(xxxx,)
from django.urls import reverse from django.http import HttpResponseRedirect def redirect_to_year(request): # ... year = 2006 # ... return HttpResponseRedirect(reverse('news-year-archive', args=(year,))) # 同redirect("/path/")
命名空间(英语:Namespace)是表示标识符的可见范围。一个标识符可在多个命名空间中定义,它在不一样命名空间中的含义是互不相干的。这样,在一个新的命名空间中可定义任何标识符,它们不会与任何已有的标识符发生冲突,由于已有的定义都处于其它命名空间中。 因为name没有做用域,Django在反解URL时,会在项目全局顺序搜索,当查找到第一个name指定URL时,当即返回 咱们在开发项目时,会常常使用name属性反解出URL,当不当心在不一样的app的urls中定义相同的name时,可能会致使URL反解错误,为了不这种事情发生,引入了命名空间。
project的urls.py:
urlpatterns = [ re_path(r'^admin/', admin.site.urls), re_path(r'^app01/', include("app01.urls",namespace="app01")), re_path(r'^app02/', include("app02.urls",namespace="app02")), ]
app01.urls:
urlpatterns = [ re_path(r'^index/', index,name="index"), ]
app02.urls:
urlpatterns = [ re_path(r'^index/', index,name="index"), ]
app01.views
from django.core.urlresolvers import reverse def index(request): return HttpResponse(reverse("app01:index"))
app02.views
from django.core.urlresolvers import reverse def index(request): return HttpResponse(reverse("app02:index"))
URLconf的示例:
from django.urls import path from . import views urlpatterns = [ path('articles/2003', views.special_case_2003) path('articles/<int:year>/', views.year_archive), path('articles/<int:year>/<int:month>', views.month_archive), ]
path字符串中,可使用<>获取符合条件的字符串,转换成对应数据类型传递给views处理函数中的变量名,例如
路径匹配类型包含如下几种:
uuid:匹配格式化的UUID,即必须包含-,字母小写。
path:匹配包含/的非空字符串。
注册自定义转化器:
对于一些复杂或者复用的须要,能够定义本身的转化器。转化器是一个类或接口,它的要求有三点:
class FourDigitYearConverter: regex = '[0-9]{4}' def to_python(self, value): return int(value) def to_url(self, value): return '%04d' % value
使用register_converter 将其注册到URL配置中:
from django.urls import register_converter, path from . import converters, views register_converter(converters.FourDigitYearConverter, 'yyyy') urlpatterns = [ path('articles/2003/', views.special_case_2003), path('articles/<yyyy:year>/', views.year_archive), ... ]