1.路由的写法
路由其实就是url与函数的对应关系。
web/urls.py
from django.urls import path,re_path
from apps.api_server import views
urlpatterns = [
# 传统写法
path('api/', views.data_list),
path('api/<int:data_id>/',views.data),
# 正则写法
re_path(r'api/(\d+)/(\w+)/',views.data_algorithm),
re_path(r'api/(?P<algorithm>\w+)/(?P<data_id>\d+)/',views.data_algorithm),
]
apps/api_server/views.py
from django.shortcuts import render,HttpResponse
def data_list(request):
return HttpResponse("data list")
def data(request,data_id):
return HttpResponse(str(data_id)+' data')
def data_algorithm(request,data_id,algorithm):
return HttpResponse(str(data_id)+' data-'+algorithm )
2.路由分发
开发时当功能越来越多时,把路由写在同一个文件里会非常乱,这时就需要用路由分发。
include分发
web/urls.py
from django.urls import path,include
urlpatterns = [
path('api/',include("apps.api_server.urls"))
]
apps/api_server/urls.py
from django.urls import path, re_path,include
from apps.api_server import views
urlpatterns = [
# 传统写法
path('', views.data_list),
path('<int:data_id>/', views.data),
# 正则写法
re_path(r'(\d+)/(\w+)/', views.data_algorithm),
re_path(r'(?P<algorithm>\w+)/(?P<data_id>\d+)/', views.data_algorithm),
#
path('data/',include([
path('add/',views.data_list),
path('delete/',views.data_list),
],None)),
]
手动分发
web/urls.py
from django.urls import path,include
urlpatterns = [
path('api/',("apps.api_server.urls",None,None))
]
apps/api_server/urls.py
from django.urls import path, re_path
from apps.api_server import views
urlpatterns = [
# 传统写法
path('', views.data_list),
path('<int:data_id>/', views.data),
# 正则写法
re_path(r'(\d+)/(\w+)/', views.data_algorithm),
re_path(r'(?P<algorithm>\w+)/(?P<data_id>\d+)/', views.data_algorithm),
#
path('data/',([
path('add/',views.data_list),
path('delete/',views.data_list),
],None,None)),
]
3.路由的常用参数
name
给路由起个名字
一般有一下两种用法
- 可以生成URL
# apps/api/urls.py
urlpatterns = [
re_path(r'auth/(\d+)/(\w)',views.auth,name = "v1"),
path('get_data/<int:data_id>/',views.get_data,name='v2')
]
# apps/api/views.py
def login(request,user_id):
url = reverse('v2',kwargs={'data_id':user_id})
return HttpResponse(url)
def get_data(request,data_id):
url = reverse('v1',args=(111,'ABC'))
return HttpResponse(url)
- HTML模版
<body>
<a href="{% url v2 data_id=2 %}">转</a>
<a href="{% url v1 111 "ABC" %}">转</a>
</body>
namespace
在不同的app中,难免会遇到重名的情况,这个功能可以划分出不同的命名区域,从而解决这一问题。
# web/urls.py
urlpatterns = [
path('api/',include("apps.api.urls",namespace='api'))
path('visual/',include("apps.visual.urls",namespace='visual'))
]
# apps/api/urls.py
urlpatterns = [
path('auth/',views.auth,name = "auth"))
]
app_name = 'api'
# apps/visual/urls.py
urlpatterns = [
path('auth/',views.auth,name = "auth"))
]
app_name = 'visual'
# 以后在url或者反向生成时
visual_url = reverse("visual:auth")
api_url = reverse("api:auth")
拓展:
- namespace需要设置app_name
- 手动分发
urlpatterns = [
path('api/',([
path('auth/',views.auth,name = "auth"))
],'api','api')
]
嵌套:
urlpatterns = [
path('api/',([
path('data',([
path('auth/',views.auth,name = "auth")) # api:data:auth
],'data','data'))
],'api','api')
]