Update python code blocks for syntax highlighting

This commit is contained in:
Raphael Pierzina 2016-12-02 15:29:29 +00:00
parent c7801144e9
commit 88738ae05c

View File

@ -65,6 +65,7 @@ Once you've set up a database and initial user created and ready to go, open up
First up we're going to define some serializers. Let's create a new module named `tutorial/quickstart/serializers.py` that we'll use for our data representations. First up we're going to define some serializers. Let's create a new module named `tutorial/quickstart/serializers.py` that we'll use for our data representations.
```python
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from rest_framework import serializers from rest_framework import serializers
@ -79,6 +80,7 @@ First up we're going to define some serializers. Let's create a new module named
class Meta: class Meta:
model = Group model = Group
fields = ('url', 'name') fields = ('url', 'name')
```
Notice that we're using hyperlinked relations in this case, with `HyperlinkedModelSerializer`. You can also use primary key and various other relationships, but hyperlinking is good RESTful design. Notice that we're using hyperlinked relations in this case, with `HyperlinkedModelSerializer`. You can also use primary key and various other relationships, but hyperlinking is good RESTful design.
@ -86,6 +88,7 @@ Notice that we're using hyperlinked relations in this case, with `HyperlinkedMod
Right, we'd better write some views then. Open `tutorial/quickstart/views.py` and get typing. Right, we'd better write some views then. Open `tutorial/quickstart/views.py` and get typing.
```python
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from rest_framework import viewsets from rest_framework import viewsets
from tutorial.quickstart.serializers import UserSerializer, GroupSerializer from tutorial.quickstart.serializers import UserSerializer, GroupSerializer
@ -105,6 +108,7 @@ Right, we'd better write some views then. Open `tutorial/quickstart/views.py` a
""" """
queryset = Group.objects.all() queryset = Group.objects.all()
serializer_class = GroupSerializer serializer_class = GroupSerializer
```
Rather than write multiple views we're grouping together all the common behavior into classes called `ViewSets`. Rather than write multiple views we're grouping together all the common behavior into classes called `ViewSets`.
@ -114,6 +118,7 @@ We can easily break these down into individual views if we need to, but using vi
Okay, now let's wire up the API URLs. On to `tutorial/urls.py`... Okay, now let's wire up the API URLs. On to `tutorial/urls.py`...
```python
from django.conf.urls import url, include from django.conf.urls import url, include
from rest_framework import routers from rest_framework import routers
from tutorial.quickstart import views from tutorial.quickstart import views
@ -128,6 +133,7 @@ Okay, now let's wire up the API URLs. On to `tutorial/urls.py`...
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'))
] ]
```
Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class. Because we're using viewsets instead of views, we can automatically generate the URL conf for our API, by simply registering the viewsets with a router class.
@ -139,6 +145,7 @@ Finally, we're including default login and logout views for use with the browsab
We'd also like to set a few global settings. We'd like to turn on pagination, and we want our API to only be accessible to admin users. The settings module will be in `tutorial/settings.py` We'd also like to set a few global settings. We'd like to turn on pagination, and we want our API to only be accessible to admin users. The settings module will be in `tutorial/settings.py`
```python
INSTALLED_APPS = ( INSTALLED_APPS = (
... ...
'rest_framework', 'rest_framework',
@ -148,6 +155,7 @@ We'd also like to set a few global settings. We'd like to turn on pagination, a
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',), 'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAdminUser',),
'PAGE_SIZE': 10 'PAGE_SIZE': 10
} }
```
Okay, we're done. Okay, we're done.