Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 57b2ba0

Browse files
Add instructions about REST API
1 parent 27d5463 commit 57b2ba0

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ This course mainly focuses on [Python](https://www.python.org/), [Django](https:
4141
8. [Django Authentication](units/django/8/auth.md)
4242
9. [TodoApp Finalization](units/django/9/finalization.md)
4343
10. [Add Hashtags](units/django/10/hashtags.md)
44-
11. REST API Development
44+
11. [REST API Development](units/django/11/rest-api.md)
4545
12. Application Deployment to Heroku
4646

4747

‎units/django/11/rest-api.md‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
REST API Development
2+
=====================
3+
[Home](https://github.com/kabirbaidhya/learn-python-django-web) | [← Prev](https://github.com/kabirbaidhya/learn-python-django-web/blob/master/units/django/10/hashtags.md) | [Next →]()
4+
5+
## API Development
6+
So far we've covered about developing a CRUD Web Application using Django and we have used a traditional MVC approach on developing the TodoApp. Now we'll see how we can expose a REST API for our TodoApp.
7+
8+
To understand more about what REST is, you can check this [stackoverflow thread](https://stackoverflow.com/questions/671118/what-exactly-is-restful-programming#answer-671132).
9+
10+
## Setting Up
11+
Firstly we'll need to add a new package `djangorestframework` into our django application. As django by default isn't REST friendly for developing application we need to add this new package to make developing RESTful applications easier.
12+
13+
```bash
14+
$ pip install djangorestframework
15+
```
16+
17+
After you've installed this, go and add `rest_framework` on the `INSTALLED_APPS` list in the `settings.py` file:
18+
```python
19+
INSTALLED_APPS = [
20+
'django.contrib.sessions',
21+
'django.contrib.messages',
22+
'django.contrib.staticfiles',
23+
'rest_framework',
24+
'todos'
25+
]
26+
```
27+
28+
## Serialization
29+
Since our REST API is going to be fully JSON based, we'll need to add serializer classes which will automatically handle the serialization/deserialization task for us.
30+
31+
So, go and create a new file `serializers.py` under our `todos` directory:
32+
33+
```python
34+
from rest_framework import serializers
35+
from .models import Todo
36+
37+
38+
class TodoSerializer(serializers.ModelSerializer):
39+
''' Serializer to map the Todo model to JSON. '''
40+
41+
class Meta:
42+
"""Meta class to map serializer's fields with the model fields."""
43+
model = Todo
44+
fields = ('id', 'title', 'description', 'completed', 'created_at', 'user')
45+
read_only_fields = ('created_at', 'id')
46+
```
47+
In the above code, we've just created a new serializer `TodoSerializer` for our `Todo` model class.
48+
49+
## Adding a ListView
50+
In RESTful API routes there are generally two kinds of endpoints: collection endpoints and resource endpoints.
51+
52+
The collection endpoints are those which would return a list of resources on `GET` and which would create a new resource on `POST` and other HTTP verbs like `OPTIONS`, `HEAD` etc are supported as always.
53+
54+
Our our TodoApp todos collection endpoint would look like this:
55+
```
56+
GET /api/todos
57+
POST /api/todos
58+
```
59+
So, to handle these we'll create a new view `TodoListView` since it's a collection endpoint. Go to your `views.py` and add a new class `TodoListView`.
60+
61+
```python
62+
from rest_framework import generics
63+
from todos.models import Todo
64+
from todos.serializers import TodoSerializer
65+
66+
class TodoListView(generics.ListCreateAPIView):
67+
queryset = Todo.objects.all()
68+
serializer_class = TodoSerializer
69+
```
70+
71+
Now this view will be responsible for creating a new todo item when it gets a `POST` request with payload and will return a list of todo items (in JSON) when it gets a `GET` request.
72+
73+
We'll still need to add a new url that points to this view, let's add this new url for `/api/todos` in our `urls.py` file:
74+
75+
```python
76+
77+
from todos import views
78+
79+
urlpatterns = [
80+
...
81+
url(r'^api/todos$', views.TodoListView.as_view(), name='api_todo_list')
82+
]
83+
```
84+
Now that we have our todo list API ready, let's go and test it using Postman.
85+
86+
You can go and send a `GET` request on `http://localhost:8000/api/todos` and you should receive a list of todo items in JSON as a response.
87+
88+
And if you try a `POST` request on the same url `http://localhost:8000/api/todos` with a JSON payload of data you want to create, you'll see that it creates a new item.
89+
90+
91+
## Read More
92+
93+
1. https://www.slideshare.net/beveganbevegan/rest-46394978
94+
2. http://www.django-rest-framework.org/#tutorial
95+
3. https://scotch.io/tutorials/build-a-rest-api-with-django-a-test-driven-approach-part-1

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /