mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Latest docs deploy
This commit is contained in:
parent
435ba7e5a8
commit
623304a19d
|
@ -258,7 +258,7 @@ Set to false if this field is not required to be present during deserialization.
|
|||
<p>A dictionary of error codes to error messages.</p>
|
||||
<h3 id="widget"><code>widget</code></h3>
|
||||
<p>Used only if rendering the field to HTML.
|
||||
This argument sets the widget that should be used to render the field.</p>
|
||||
This argument sets the widget that should be used to render the field. For more details, and a list of available widgets, see <a href="https://docs.djangoproject.com/en/dev/ref/forms/widgets/">the Django documentation on form widgets</a>. </p>
|
||||
<h3 id="label"><code>label</code></h3>
|
||||
<p>A short text string that may be used as the name of the field in HTML form fields or other descriptive elements.</p>
|
||||
<h3 id="help_text"><code>help_text</code></h3>
|
||||
|
|
|
@ -216,10 +216,6 @@ a.fusion-poweredby {
|
|||
<img src="https://secure.travis-ci.org/tomchristie/django-rest-framework.png?branch=master" class="travis-build-image">
|
||||
</p>
|
||||
|
||||
<hr />
|
||||
<h4 id="django-rest-framework-3-kickstarter-announcement">Django REST framework 3 - Kickstarter announcement!</h4>
|
||||
<p>We are currently running a Kickstarter campaign to help fund the development of Django REST framework 3.</p>
|
||||
<p>If you want to help drive sustainable open-source development <strong>please <a href="https://www.kickstarter.com/projects/tomchristie/django-rest-framework-3">check out the Kickstarter project</a> and consider funding us.</strong></p>
|
||||
<hr />
|
||||
<p>
|
||||
<h1 style="position: absolute;
|
||||
|
|
|
@ -356,7 +356,7 @@ serializer.data
|
|||
content
|
||||
# '{"pk": 2, "title": "", "code": "print \\"hello, world\\"\\n", "linenos": false, "language": "python", "style": "friendly"}'
|
||||
</code></pre>
|
||||
<p>Deserialization is similar. First we parse a stream into Python native datatypes... </p>
|
||||
<p>Deserialization is similar. First we parse a stream into Python native datatypes...</p>
|
||||
<pre class="prettyprint lang-py"><code># 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)
|
||||
</code></pre>
|
||||
<p>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 <code>csrf_exempt</code>. 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. </p>
|
||||
<p>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 <code>csrf_exempt</code>. 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.</p>
|
||||
<p>We'll also need a view which corresponds to an individual snippet, and can be used to retrieve, update or delete the snippet.</p>
|
||||
<pre class="prettyprint lang-py"><code>@csrf_exempt
|
||||
def snippet_detail(request, pk):
|
||||
|
|
|
@ -226,7 +226,7 @@ request.DATA # Handles arbitrary data. Works for 'POST', 'PUT' and 'PATCH' met
|
|||
<p>These wrappers provide a few bits of functionality such as making sure you receive <code>Request</code> instances in your view, and adding context to <code>Response</code> objects so that content negotiation can be performed.</p>
|
||||
<p>The wrappers also provide behaviour such as returning <code>405 Method Not Allowed</code> responses when appropriate, and handling any <code>ParseError</code> exception that occurs when accessing <code>request.DATA</code> with malformed input.</p>
|
||||
<h2 id="pulling-it-all-together">Pulling it all together</h2>
|
||||
<p>Okay, let's go ahead and start using these new components to write a few views. </p>
|
||||
<p>Okay, let's go ahead and start using these new components to write a few views.</p>
|
||||
<p>We don't need our <code>JSONResponse</code> class in <code>views.py</code> anymore, so go ahead and delete that. Once that's done we can start refactoring our views slightly.</p>
|
||||
<pre class="prettyprint lang-py"><code>from rest_framework import status
|
||||
from rest_framework.decorators import api_view
|
||||
|
|
|
@ -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)
|
||||
</code></pre>
|
||||
<p>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 <code>views.py</code>. </p>
|
||||
<p>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 <code>views.py</code>.</p>
|
||||
<pre class="prettyprint lang-py"><code>class SnippetDetail(APIView):
|
||||
"""
|
||||
Retrieve, update or delete a snippet instance.
|
||||
|
|
|
@ -304,7 +304,7 @@ url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
|
|||
</code></pre>
|
||||
<h2 id="adding-login-to-the-browsable-api">Adding login to the Browsable API</h2>
|
||||
<p>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.</p>
|
||||
<p>We can add a login view for use with the browsable API, by editing the URLconf in our project-level urls.py file.</p>
|
||||
<p>We can add a login view for use with the browsable API, by editing the URLconf in our project-level <code>urls.py</code> file.</p>
|
||||
<p>Add the following import at the top of the file:</p>
|
||||
<pre class="prettyprint lang-py"><code>from django.conf.urls import include
|
||||
</code></pre>
|
||||
|
|
|
@ -202,9 +202,9 @@ a.fusion-poweredby {
|
|||
|
||||
<div id="main-content" class="span9">
|
||||
<h1 id="tutorial-5-relationships-hyperlinked-apis">Tutorial 5: Relationships & Hyperlinked APIs</h1>
|
||||
<p>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. </p>
|
||||
<p>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.</p>
|
||||
<h2 id="creating-an-endpoint-for-the-root-of-our-api">Creating an endpoint for the root of our API</h2>
|
||||
<p>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 <code>@api_view</code> decorator we introduced earlier.</p>
|
||||
<p>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 <code>@api_view</code> decorator we introduced earlier. In your <code>snippets/views.py</code> add:</p>
|
||||
<pre class="prettyprint lang-py"><code>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):
|
|||
<p>The other obvious thing that's still missing from our pastebin API is the code highlighting endpoints.</p>
|
||||
<p>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.</p>
|
||||
<p>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.</p>
|
||||
<p>Instead of using a concrete generic view, we'll use the base class for representing instances, and create our own <code>.get()</code> method. In your <code>snippets.views</code> add:</p>
|
||||
<p>Instead of using a concrete generic view, we'll use the base class for representing instances, and create our own <code>.get()</code> method. In your <code>snippets/views.py</code> add:</p>
|
||||
<pre class="prettyprint lang-py"><code>from rest_framework import renderers
|
||||
from rest_framework.response import Response
|
||||
|
||||
|
@ -236,7 +236,7 @@ class SnippetHighlight(generics.GenericAPIView):
|
|||
return Response(snippet.highlighted)
|
||||
</code></pre>
|
||||
<p>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:</p>
|
||||
We'll add a url pattern for our new API root in <code>snippets/urls.py</code>:</p>
|
||||
<pre class="prettyprint lang-py"><code>url(r'^$', 'api_root'),
|
||||
</code></pre>
|
||||
<p>And then add a url pattern for the snippet highlights:</p>
|
||||
|
@ -261,7 +261,7 @@ We'll add a url pattern for our new API root:</p>
|
|||
<li>Relationships use <code>HyperlinkedRelatedField</code>,
|
||||
instead of <code>PrimaryKeyRelatedField</code>.</li>
|
||||
</ul>
|
||||
<p>We can easily re-write our existing serializers to use hyperlinking.</p>
|
||||
<p>We can easily re-write our existing serializers to use hyperlinking. In your <code>snippets/serializers.py</code> add:</p>
|
||||
<pre class="prettyprint lang-py"><code>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):
|
|||
<li>Our user serializer includes a field that refers to <code>'snippet-detail'</code>.</li>
|
||||
<li>Our snippet and user serializers include <code>'url'</code> fields that by default will refer to <code>'{model_name}-detail'</code>, which in this case will be <code>'snippet-detail'</code> and <code>'user-detail'</code>.</li>
|
||||
</ul>
|
||||
<p>After adding all those names into our URLconf, our final <code>'urls.py'</code> file should look something like this:</p>
|
||||
<p>After adding all those names into our URLconf, our final <code>snippets/urls.py</code> file should look something like this:</p>
|
||||
<pre class="prettyprint lang-py"><code># API endpoints
|
||||
urlpatterns = format_suffix_patterns(patterns('snippets.views',
|
||||
url(r'^$', 'api_root'),
|
||||
|
|
Loading…
Reference in New Issue
Block a user