mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 19:43:59 +03:00
Merge pull request #1899 from collinanderson/urls
Remove patterns and strings from urls in tutorial.
This commit is contained in:
commit
4ffae7c0e9
|
@ -190,9 +190,10 @@ If you've already created some users, you can generate tokens for all existing u
|
||||||
|
|
||||||
When using `TokenAuthentication`, you may want to provide a mechanism for clients to obtain a token given the username and password. REST framework provides a built-in view to provide this behavior. To use it, add the `obtain_auth_token` view to your URLconf:
|
When using `TokenAuthentication`, you may want to provide a mechanism for clients to obtain a token given the username and password. REST framework provides a built-in view to provide this behavior. To use it, add the `obtain_auth_token` view to your URLconf:
|
||||||
|
|
||||||
urlpatterns += patterns('',
|
from rest_framework.authtoken import views
|
||||||
url(r'^api-token-auth/', 'rest_framework.authtoken.views.obtain_auth_token')
|
urlpatterns += [
|
||||||
)
|
url(r'^api-token-auth/', views.obtain_auth_token)
|
||||||
|
]
|
||||||
|
|
||||||
Note that the URL part of the pattern can be whatever you want to use.
|
Note that the URL part of the pattern can be whatever you want to use.
|
||||||
|
|
||||||
|
|
|
@ -26,12 +26,13 @@ Arguments:
|
||||||
Example:
|
Example:
|
||||||
|
|
||||||
from rest_framework.urlpatterns import format_suffix_patterns
|
from rest_framework.urlpatterns import format_suffix_patterns
|
||||||
|
from blog import views
|
||||||
|
|
||||||
urlpatterns = patterns('blog.views',
|
urlpatterns = [
|
||||||
url(r'^/$', 'api_root'),
|
url(r'^/$', views.apt_root),
|
||||||
url(r'^comments/$', 'comment_list'),
|
url(r'^comments/$', views.comment_list),
|
||||||
url(r'^comments/(?P<pk>[0-9]+)/$', 'comment_detail')
|
url(r'^comments/(?P<pk>[0-9]+)/$', views.comment_detail)
|
||||||
)
|
]
|
||||||
|
|
||||||
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html'])
|
urlpatterns = format_suffix_patterns(urlpatterns, allowed=['json', 'html'])
|
||||||
|
|
||||||
|
|
|
@ -85,10 +85,10 @@ Add `'rest_framework'` to your `INSTALLED_APPS` setting.
|
||||||
|
|
||||||
If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root `urls.py` file.
|
If you're intending to use the browsable API you'll probably also want to add REST framework's login and logout views. Add the following to your root `urls.py` file.
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = [
|
||||||
...
|
...
|
||||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||||
)
|
]
|
||||||
|
|
||||||
Note that the URL path can be whatever you want, but you must include `'rest_framework.urls'` with the `'rest_framework'` namespace.
|
Note that the URL path can be whatever you want, but you must include `'rest_framework.urls'` with the `'rest_framework'` namespace.
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,7 @@ As an example of just how simple REST framework APIs can now be, here's an API w
|
||||||
"""
|
"""
|
||||||
A REST framework API for viewing and editing users and groups.
|
A REST framework API for viewing and editing users and groups.
|
||||||
"""
|
"""
|
||||||
from django.conf.urls.defaults import url, patterns, include
|
from django.conf.urls.defaults import url, include
|
||||||
from django.contrib.auth.models import User, Group
|
from django.contrib.auth.models import User, Group
|
||||||
from rest_framework import viewsets, routers
|
from rest_framework import viewsets, routers
|
||||||
|
|
||||||
|
@ -36,10 +36,10 @@ As an example of just how simple REST framework APIs can now be, here's an API w
|
||||||
|
|
||||||
# Wire up our API using automatic URL routing.
|
# Wire up our API using automatic URL routing.
|
||||||
# Additionally, we include login URLs for the browseable API.
|
# Additionally, we include login URLs for the browseable API.
|
||||||
urlpatterns = patterns('',
|
urlpatterns = [
|
||||||
url(r'^', include(router.urls)),
|
url(r'^', include(router.urls)),
|
||||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||||
)
|
]
|
||||||
|
|
||||||
The best place to get started with ViewSets and Routers is to take a look at the [newest section in the tutorial][part-6], which demonstrates their usage.
|
The best place to get started with ViewSets and Routers is to take a look at the [newest section in the tutorial][part-6], which demonstrates their usage.
|
||||||
|
|
||||||
|
|
|
@ -64,9 +64,9 @@ We'll also need to add our new `snippets` app and the `rest_framework` app to `I
|
||||||
|
|
||||||
We also need to wire up the root urlconf, in the `tutorial/urls.py` file, to include our snippet app's URLs.
|
We also need to wire up the root urlconf, in the `tutorial/urls.py` file, to include our snippet app's URLs.
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = [
|
||||||
url(r'^', include('snippets.urls')),
|
url(r'^', include('snippets.urls')),
|
||||||
)
|
]
|
||||||
|
|
||||||
Okay, we're ready to roll.
|
Okay, we're ready to roll.
|
||||||
|
|
||||||
|
@ -297,11 +297,12 @@ 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:
|
Finally we need to wire these views up. Create the `snippets/urls.py` file:
|
||||||
|
|
||||||
from django.conf.urls import patterns, url
|
from django.conf.urls import patterns, url
|
||||||
|
from snippets import views
|
||||||
|
|
||||||
urlpatterns = patterns('snippets.views',
|
urlpatterns = [
|
||||||
url(r'^snippets/$', 'snippet_list'),
|
url(r'^snippets/$', views.snippet_list),
|
||||||
url(r'^snippets/(?P<pk>[0-9]+)/$', 'snippet_detail'),
|
url(r'^snippets/(?P<pk>[0-9]+)/$', views.snippet_detail),
|
||||||
)
|
]
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
|
|
@ -110,11 +110,12 @@ Now update the `urls.py` file slightly, to append a set of `format_suffix_patter
|
||||||
|
|
||||||
from django.conf.urls import patterns, url
|
from django.conf.urls import patterns, url
|
||||||
from rest_framework.urlpatterns import format_suffix_patterns
|
from rest_framework.urlpatterns import format_suffix_patterns
|
||||||
|
from snippets import views
|
||||||
|
|
||||||
urlpatterns = patterns('snippets.views',
|
urlpatterns = [
|
||||||
url(r'^snippets/$', 'snippet_list'),
|
url(r'^snippets/$', views.snippet_list),
|
||||||
url(r'^snippets/(?P<pk>[0-9]+)$', 'snippet_detail'),
|
url(r'^snippets/(?P<pk>[0-9]+)$', views.snippet_detail),
|
||||||
)
|
]
|
||||||
|
|
||||||
urlpatterns = format_suffix_patterns(urlpatterns)
|
urlpatterns = format_suffix_patterns(urlpatterns)
|
||||||
|
|
||||||
|
|
|
@ -68,10 +68,10 @@ We'll also need to refactor our `urls.py` slightly now we're using class based v
|
||||||
from rest_framework.urlpatterns import format_suffix_patterns
|
from rest_framework.urlpatterns import format_suffix_patterns
|
||||||
from snippets import views
|
from snippets import views
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = [
|
||||||
url(r'^snippets/$', views.SnippetList.as_view()),
|
url(r'^snippets/$', views.SnippetList.as_view()),
|
||||||
url(r'^snippets/(?P<pk>[0-9]+)/$', views.SnippetDetail.as_view()),
|
url(r'^snippets/(?P<pk>[0-9]+)/$', views.SnippetDetail.as_view()),
|
||||||
)
|
]
|
||||||
|
|
||||||
urlpatterns = format_suffix_patterns(urlpatterns)
|
urlpatterns = format_suffix_patterns(urlpatterns)
|
||||||
|
|
||||||
|
|
|
@ -137,10 +137,10 @@ Add the following import at the top of the file:
|
||||||
|
|
||||||
And, at the end of the file, add a pattern to include the login and logout views for the browsable API.
|
And, at the end of the file, add a pattern to include the login and logout views for the browsable API.
|
||||||
|
|
||||||
urlpatterns += patterns('',
|
urlpatterns += [
|
||||||
url(r'^api-auth/', include('rest_framework.urls',
|
url(r'^api-auth/', include('rest_framework.urls',
|
||||||
namespace='rest_framework')),
|
namespace='rest_framework')),
|
||||||
)
|
]
|
||||||
|
|
||||||
The `r'^api-auth/'` part of pattern can actually be whatever URL you want to use. The only restriction is that the included urls must use the `'rest_framework'` namespace.
|
The `r'^api-auth/'` part of pattern can actually be whatever URL you want to use. The only restriction is that the included urls must use the `'rest_framework'` namespace.
|
||||||
|
|
||||||
|
|
|
@ -108,8 +108,8 @@ If we're going to have a hyperlinked API, we need to make sure we name our URL p
|
||||||
After adding all those names into our URLconf, our final `snippets/urls.py` file should look something like this:
|
After adding all those names into our URLconf, our final `snippets/urls.py` file should look something like this:
|
||||||
|
|
||||||
# API endpoints
|
# API endpoints
|
||||||
urlpatterns = format_suffix_patterns(patterns('snippets.views',
|
urlpatterns = format_suffix_patterns([
|
||||||
url(r'^$', 'api_root'),
|
url(r'^$', views.api_root),
|
||||||
url(r'^snippets/$',
|
url(r'^snippets/$',
|
||||||
views.SnippetList.as_view(),
|
views.SnippetList.as_view(),
|
||||||
name='snippet-list'),
|
name='snippet-list'),
|
||||||
|
@ -125,13 +125,13 @@ After adding all those names into our URLconf, our final `snippets/urls.py` file
|
||||||
url(r'^users/(?P<pk>[0-9]+)/$',
|
url(r'^users/(?P<pk>[0-9]+)/$',
|
||||||
views.UserDetail.as_view(),
|
views.UserDetail.as_view(),
|
||||||
name='user-detail')
|
name='user-detail')
|
||||||
))
|
])
|
||||||
|
|
||||||
# Login and logout views for the browsable API
|
# Login and logout views for the browsable API
|
||||||
urlpatterns += patterns('',
|
urlpatterns += [
|
||||||
url(r'^api-auth/', include('rest_framework.urls',
|
url(r'^api-auth/', include('rest_framework.urls',
|
||||||
namespace='rest_framework')),
|
namespace='rest_framework')),
|
||||||
)
|
]
|
||||||
|
|
||||||
## Adding pagination
|
## Adding pagination
|
||||||
|
|
||||||
|
|
|
@ -87,14 +87,14 @@ Notice how we're creating multiple views from each `ViewSet` class, by binding t
|
||||||
|
|
||||||
Now that we've bound our resources into concrete views, we can register the views with the URL conf as usual.
|
Now that we've bound our resources into concrete views, we can register the views with the URL conf as usual.
|
||||||
|
|
||||||
urlpatterns = format_suffix_patterns(patterns('snippets.views',
|
urlpatterns = format_suffix_patterns([
|
||||||
url(r'^$', 'api_root'),
|
url(r'^$', api_root),
|
||||||
url(r'^snippets/$', snippet_list, name='snippet-list'),
|
url(r'^snippets/$', snippet_list, name='snippet-list'),
|
||||||
url(r'^snippets/(?P<pk>[0-9]+)/$', snippet_detail, name='snippet-detail'),
|
url(r'^snippets/(?P<pk>[0-9]+)/$', snippet_detail, name='snippet-detail'),
|
||||||
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
|
url(r'^snippets/(?P<pk>[0-9]+)/highlight/$', snippet_highlight, name='snippet-highlight'),
|
||||||
url(r'^users/$', user_list, name='user-list'),
|
url(r'^users/$', user_list, name='user-list'),
|
||||||
url(r'^users/(?P<pk>[0-9]+)/$', user_detail, name='user-detail')
|
url(r'^users/(?P<pk>[0-9]+)/$', user_detail, name='user-detail')
|
||||||
))
|
])
|
||||||
|
|
||||||
## Using Routers
|
## Using Routers
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ Because we're using `ViewSet` classes rather than `View` classes, we actually do
|
||||||
|
|
||||||
Here's our re-wired `urls.py` file.
|
Here's our re-wired `urls.py` file.
|
||||||
|
|
||||||
from django.conf.urls import patterns, url, include
|
from django.conf.urls import url, include
|
||||||
from snippets import views
|
from snippets import views
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
|
@ -113,10 +113,10 @@ Here's our re-wired `urls.py` file.
|
||||||
|
|
||||||
# The API URLs are now determined automatically by the router.
|
# The API URLs are now determined automatically by the router.
|
||||||
# Additionally, we include the login URLs for the browseable API.
|
# Additionally, we include the login URLs for the browseable API.
|
||||||
urlpatterns = patterns('',
|
urlpatterns = [
|
||||||
url(r'^', include(router.urls)),
|
url(r'^', include(router.urls)),
|
||||||
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework'))
|
||||||
)
|
]
|
||||||
|
|
||||||
Registering the viewsets with the router is similar to providing a urlpattern. We include two arguments - the URL prefix for the views, and the viewset itself.
|
Registering the viewsets with the router is similar to providing a urlpattern. We include two arguments - the URL prefix for the views, and the viewset itself.
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user