官术网_书友最值得收藏!

  • Django RESTful Web Services
  • Gaston C. Hillar
  • 224字
  • 2021-06-30 19:30:59

Routing URLs to Django views and functions

Now, we have to create a new Python file named urls.py in the toys folder, specifically, the toys/urls.py file. The following lines show the code for this file, which defines the URL patterns that specify the regular expressions that have to be matched in the request to run a specific function previously defined in the views.py file. The code file for the sample is included in the hillar_django_restful_03_01 folder, in the restful01/toys/urls.py file:

from django.conf.urls import url 
from toys import views 
 
urlpatterns = [ 
    url(r'^toys/$', views.toy_list), 
    url(r'^toys/(?P<pk>[0-9]+)$', views.toy_detail), 
] 

The urlpatterns list makes it possible to route URLs to views. The code calls the django.conf.urls.url function with the regular expression that has to be matched and the view function defined in the views module as arguments to create a RegexURLPattern instance for each entry in the urlpatterns list.

Now, we have to replace the code in the urls.py file in the restful01 folder, specifically, the restful01/urls.py file. The file defines the root URL configurations, and therefore we must include the URL patterns declared in the previously coded toys/urls.py file. The following lines show the new code for the restful01/urls.py file. The code file for the sample is included in the hillar_django_restful_03_01 folder, in the restful01/urls.py file:

from django.conf.urls import url, include 
 
urlpatterns = [ 
    url(r'^', include('toys.urls')), 
] 
主站蜘蛛池模板: 乌什县| 扶绥县| 永年县| 临海市| 桑日县| 东乌珠穆沁旗| 新晃| 夏邑县| 洮南市| 临沧市| 二手房| 七台河市| 广丰县| 兴海县| 嫩江县| 安远县| 会昌县| 遂昌县| 南汇区| 牡丹江市| 内黄县| 石楼县| 依兰县| 南昌市| 桐庐县| 黔南| 高清| 太谷县| 五峰| 普定县| 获嘉县| 容城县| 白沙| 平乐县| 山东省| 嘉峪关市| 青岛市| 湘西| 周宁县| 黄陵县| 高青县|