diff --git a/api-guide/fields.html b/api-guide/fields.html index ad29a6cb7..72ef27e40 100644 --- a/api-guide/fields.html +++ b/api-guide/fields.html @@ -258,7 +258,7 @@ Set to false if this field is not required to be present during deserialization.

A dictionary of error codes to error messages.

widget

Used only if rendering the field to HTML. -This argument sets the widget that should be used to render the field.

+This argument sets the widget that should be used to render the field. For more details, and a list of available widgets, see the Django documentation on form widgets.

label

A short text string that may be used as the name of the field in HTML form fields or other descriptive elements.

help_text

diff --git a/index.html b/index.html index e2cea0aa0..f58cdff1a 100644 --- a/index.html +++ b/index.html @@ -216,10 +216,6 @@ a.fusion-poweredby {

-
-

Django REST framework 3 - Kickstarter announcement!

-

We are currently running a Kickstarter campaign to help fund the development of Django REST framework 3.

-

If you want to help drive sustainable open-source development please check out the Kickstarter project and consider funding us.


-

Deserialization is similar. First we parse a stream into Python native datatypes...

+

Deserialization is similar. First we parse a stream into Python native datatypes...

# This import will use either `StringIO.StringIO` or `io.BytesIO`
 # as appropriate, depending on if we're running Python 2 or Python 3.
 from rest_framework.compat import BytesIO
@@ -427,7 +427,7 @@ def snippet_list(request):
             return JSONResponse(serializer.data, status=201)
         return JSONResponse(serializer.errors, status=400)
 
-

Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as csrf_exempt. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.

+

Note that because we want to be able to POST to this view from clients that won't have a CSRF token we need to mark the view as csrf_exempt. This isn't something that you'd normally want to do, and REST framework views actually use more sensible behavior than this, but it'll do for our purposes right now.

We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.

@csrf_exempt
 def snippet_detail(request, pk):
diff --git a/tutorial/2-requests-and-responses.html b/tutorial/2-requests-and-responses.html
index f1d0cb90f..7dd66190c 100644
--- a/tutorial/2-requests-and-responses.html
+++ b/tutorial/2-requests-and-responses.html
@@ -226,7 +226,7 @@ request.DATA  # Handles arbitrary data.  Works for 'POST', 'PUT' and 'PATCH' met
 

These wrappers provide a few bits of functionality such as making sure you receive Request instances in your view, and adding context to Response objects so that content negotiation can be performed.

The wrappers also provide behaviour such as returning 405 Method Not Allowed responses when appropriate, and handling any ParseError exception that occurs when accessing request.DATA with malformed input.

Pulling it all together

-

Okay, let's go ahead and start using these new components to write a few views.

+

Okay, let's go ahead and start using these new components to write a few views.

We don't need our JSONResponse class in views.py anymore, so go ahead and delete that. Once that's done we can start refactoring our views slightly.

from rest_framework import status
 from rest_framework.decorators import api_view
@@ -258,7 +258,7 @@ def snippet_list(request):
 def snippet_detail(request, pk):
     """
     Retrieve, update or delete a snippet instance.
-    """              
+    """
     try:
         snippet = Snippet.objects.get(pk=pk)
     except Snippet.DoesNotExist:
diff --git a/tutorial/3-class-based-views.html b/tutorial/3-class-based-views.html
index 1ba32f234..b20becf31 100644
--- a/tutorial/3-class-based-views.html
+++ b/tutorial/3-class-based-views.html
@@ -226,7 +226,7 @@ class SnippetList(APIView):
             return Response(serializer.data, status=status.HTTP_201_CREATED)
         return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 
-

So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in views.py.

+

So far, so good. It looks pretty similar to the previous case, but we've got better separation between the different HTTP methods. We'll also need to update the instance view in views.py.

class SnippetDetail(APIView):
     """
     Retrieve, update or delete a snippet instance.
diff --git a/tutorial/4-authentication-and-permissions.html b/tutorial/4-authentication-and-permissions.html
index 8f59a2f5f..01d84818a 100644
--- a/tutorial/4-authentication-and-permissions.html
+++ b/tutorial/4-authentication-and-permissions.html
@@ -304,7 +304,7 @@ url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
 

Adding login to the Browsable API

If you open a browser and navigate to the browsable API at the moment, you'll find that you're no longer able to create new code snippets. In order to do so we'd need to be able to login as a user.

-

We can add a login view for use with the browsable API, by editing the URLconf in our project-level urls.py file.

+

We can add a login view for use with the browsable API, by editing the URLconf in our project-level urls.py file.

Add the following import at the top of the file:

from django.conf.urls import include
 
diff --git a/tutorial/5-relationships-and-hyperlinked-apis.html b/tutorial/5-relationships-and-hyperlinked-apis.html index ae1f9bd28..0c274c054 100644 --- a/tutorial/5-relationships-and-hyperlinked-apis.html +++ b/tutorial/5-relationships-and-hyperlinked-apis.html @@ -202,9 +202,9 @@ a.fusion-poweredby {

Tutorial 5: Relationships & Hyperlinked APIs

-

At the moment relationships within our API are represented by using primary keys. In this part of the tutorial we'll improve the cohesion and discoverability of our API, by instead using hyperlinking for relationships.

+

At the moment relationships within our API are represented by using primary keys. In this part of the tutorial we'll improve the cohesion and discoverability of our API, by instead using hyperlinking for relationships.

Creating an endpoint for the root of our API

-

Right now we have endpoints for 'snippets' and 'users', but we don't have a single entry point to our API. To create one, we'll use a regular function-based view and the @api_view decorator we introduced earlier.

+

Right now we have endpoints for 'snippets' and 'users', but we don't have a single entry point to our API. To create one, we'll use a regular function-based view and the @api_view decorator we introduced earlier. In your snippets/views.py add:

from rest_framework import renderers
 from rest_framework.decorators import api_view
 from rest_framework.response import Response
@@ -223,7 +223,7 @@ def api_root(request, format=None):
 

The other obvious thing that's still missing from our pastebin API is the code highlighting endpoints.

Unlike all our other API endpoints, we don't want to use JSON, but instead just present an HTML representation. There are two styles of HTML renderer provided by REST framework, one for dealing with HTML rendered using templates, the other for dealing with pre-rendered HTML. The second renderer is the one we'd like to use for this endpoint.

The other thing we need to consider when creating the code highlight view is that there's no existing concrete generic view that we can use. We're not returning an object instance, but instead a property of an object instance.

-

Instead of using a concrete generic view, we'll use the base class for representing instances, and create our own .get() method. In your snippets.views add:

+

Instead of using a concrete generic view, we'll use the base class for representing instances, and create our own .get() method. In your snippets/views.py add:

from rest_framework import renderers
 from rest_framework.response import Response
 
@@ -236,7 +236,7 @@ class SnippetHighlight(generics.GenericAPIView):
         return Response(snippet.highlighted)
 

As usual we need to add the new views that we've created in to our URLconf. -We'll add a url pattern for our new API root:

+We'll add a url pattern for our new API root in snippets/urls.py:

url(r'^$', 'api_root'),
 

And then add a url pattern for the snippet highlights:

@@ -261,7 +261,7 @@ We'll add a url pattern for our new API root:

  • Relationships use HyperlinkedRelatedField, instead of PrimaryKeyRelatedField.
  • -

    We can easily re-write our existing serializers to use hyperlinking.

    +

    We can easily re-write our existing serializers to use hyperlinking. In your snippets/serializers.py add:

    class SnippetSerializer(serializers.HyperlinkedModelSerializer):
         owner = serializers.Field(source='owner.username')
         highlight = serializers.HyperlinkedIdentityField(view_name='snippet-highlight', format='html')
    @@ -289,7 +289,7 @@ class UserSerializer(serializers.HyperlinkedModelSerializer):
     
  • Our user serializer includes a field that refers to 'snippet-detail'.
  • Our snippet and user serializers include 'url' fields that by default will refer to '{model_name}-detail', which in this case will be 'snippet-detail' and 'user-detail'.
  • -

    After adding all those names into our URLconf, our final '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
     urlpatterns = format_suffix_patterns(patterns('snippets.views',
         url(r'^$', 'api_root'),
    @@ -311,7 +311,7 @@ urlpatterns = format_suffix_patterns(patterns('snippets.views',
     ))
     
     # Login and logout views for the browsable API
    -urlpatterns += patterns('',    
    +urlpatterns += patterns('',
         url(r'^api-auth/', include('rest_framework.urls',
                                    namespace='rest_framework')),
     )
    diff --git a/tutorial/quickstart.html b/tutorial/quickstart.html
    index 5949e4ed8..171e7255a 100644
    --- a/tutorial/quickstart.html
    +++ b/tutorial/quickstart.html
    @@ -319,22 +319,22 @@ REST_FRAMEWORK = {
     
    python ./manage.py runserver
     

    We can now access our API, both from the command-line, using tools like curl...

    -
    bash: curl -H 'Accept: application/json; indent=4' -u admin:password http://127.0.0.1:8000/users/ 
    +
    bash: curl -H 'Accept: application/json; indent=4' -u admin:password http://127.0.0.1:8000/users/
     {
    -    "count": 2, 
    -    "next": null, 
    -    "previous": null, 
    +    "count": 2,
    +    "next": null,
    +    "previous": null,
         "results": [
             {
    -            "email": "admin@example.com", 
    -            "groups": [], 
    -            "url": "http://127.0.0.1:8000/users/1/", 
    +            "email": "admin@example.com",
    +            "groups": [],
    +            "url": "http://127.0.0.1:8000/users/1/",
                 "username": "admin"
    -        }, 
    +        },
             {
    -            "email": "tom@example.com", 
    -            "groups": [                ], 
    -            "url": "http://127.0.0.1:8000/users/2/", 
    +            "email": "tom@example.com",
    +            "groups": [                ],
    +            "url": "http://127.0.0.1:8000/users/2/",
                 "username": "tom"
             }
         ]