1.普通的路由分发

 1 # 在APP目录下新建文件:my_urls.py
 2
 3 from django.urls import path
 4 from .views import login, index
 5
 6 urlpatterns = [
 7     path('login/', login)
 8     path('home/', home),
 9 ]
10
11
12 
13 # 修改:urls.py
14
15 from django.contrib import admin
16 from django.urls import path, include
17 # 在Django2中需要添加此项
18 app_name = 'app'
19
20 urlpatterns = [
21     path('admin/', admin.site.urls),
22     path('app/', include('app.my_urls')),
23 ]

访问地址:http://localhost:8000/app/home

2.使用ModelViewSet组件后路由分发

 1 # 在APP目录下添加:my_urls.py
 2
 3 from rest_framework.routers import DefaultRouter
 4 from django.urls import path, include
 5 from .views import login, home
 6
 7 dr = DefaultRouter()
 8 dr.register('login', login)
 9 dr.register('home', home)
10
11 urlpatterns = [
12     path('', include(dr.urls)),
13 ]
14
15
16 # 修改项目中urls.py
17 from django.contrib import admin
18 from django.urls import path, include
19
20 app_name = 'app'
21
22 urlpatterns = [
23     path('admin/', admin.site.urls),
24     path('app/', include('app.my_urls')),
25 ]

访问地址:http://localhost:8000/app/home

小白起步,希望大神指出不足!

01-26 05:42