Update tutorial part 1 to Django 2.0 routing syntax (#5963)

Updated part 1 of the DRF tutorial to use django.urls.path instead of
django.conf.urls.url
This commit is contained in:
Chris 2018-05-02 17:49:26 -04:00
parent 8c03c49400
commit d78b98fc0f

View File

@ -275,20 +275,22 @@ We'll also need a view which corresponds to an individual snippet, and can be us
Finally we need to wire these views up. Create the `snippets/urls.py` file:
from django.conf.urls import url
from django.urls import path
# note that the path function was added in Django 2.0
# use django.conf.urls.url if on an older version of Django
from snippets import views
urlpatterns = [
url(r'^snippets/$', views.snippet_list),
url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail),
path('snippets/', views.snippet_list),
path('snippets/<int:pk>/', views.snippet_detail),
]
We also need to wire up the root urlconf, in the `tutorial/urls.py` file, to include our snippet app's URLs.
from django.conf.urls import url, include
from django.urls import path, include
urlpatterns = [
url(r'^', include('snippets.urls')),
path('', include('snippets.urls')),
]
It's worth noting that there are a couple of edge cases we're not dealing with properly at the moment. If we send malformed `json`, or if a request is made with a method that the view doesn't handle, then we'll end up with a 500 "server error" response. Still, this'll do for now.