1. Introduction
In the following course I would like you to learn how to build a CRUD in Django using REST Framework, a standardized tool for creating APIs with Django, and secondly, with Python.
The minimum requirements are:
- Have the most up-to-date version of Python 3 installed.
- A text editor. If you are just getting started, I recommend Pycharm Community Edition or VSCode with the Python plugin.
- Access to the terminal.
The goals to achieve are the essential ones in any API:
- Endpoint that lists books.
- Endpoint that shows us the details of a specific book.
- Endpoint to add a book.
- Endpoint to update an existing book.
- Endpoint that deletes an existing book.
- Create tests for all the Endpoints to make sure they work under any circumstances.
If you are impatient and want to try the results out right away, you can open the terminal. Below you can hit a server where a copy of the code you are going to learn is up and running.
List books
Unfortunately, Heroku announced that it would remove its free plans, which means the examples in this lesson are not available to test with
curl, since it was deployed on a free Dyno. The situation would be different if the site received enough recurring donations. As that is not the case, you can test it directly on your own machine using the code found in the GitHub repository.
curl https://example-of-crud-in-django-jrf.herokuapp.com/api/book/
Output
[
{
"id": 1,
"title": "Things Fall Apart",
"country": "Nigeria",
"year": 1958,
"author": "Chinua Achebe",
"created_at": "2021-07-15T14:27:14.927177",
"updated_at": "2021-07-15T14:27:14.927213"
},
{
"id": 2,
"title": "Fairy tales",
"country": "Denmark",
"year": 1836,
"author": "Hans Christian Andersen",
"created_at": "2021-07-15T14:27:14.930223",
"updated_at": "2021-07-15T14:27:14.930233"
},
...
Get the details of a book
curl https://example-of-crud-in-django-jrf.herokuapp.com/api/book/1/
Output
{
"id": 1,
"title": "Things Fall Apart",
"country": "Nigeria",
"year": 1958,
"author": "Chinua Achebe",
"created_at": "2021-07-15T14:27:14.927177",
"updated_at": "2021-07-15T14:27:14.927213"
}
Create a book
curl -XPOST -H "Content-type: application/json" -d '{"title": "The foundation", "country": "eeuu", "author": "Isaac Asimov", "year": "1951"}' https://example-of-crud-in-django-jrf.herokuapp.com/api/book/
Output
{
"id": 101,
"title": "The foundation",
"country": "eeuu",
"year": 1951,
"author": "Isaac Asimov",
"created_at": "2021-07-15T14:47:25.262738",
"updated_at": "2021-07-15T14:47:25.262753"
}
Update a book
curl -XPUT -H "Content-type: application/json" -d '{"title": "The End of Eternity", "country": "eeuu", "author": "Isaac Asimov", "year": "1955"}' https://example-of-crud-in-django-jrf.herokuapp.com/api/book/1/
Output
{
"id": 1,
"title": "The End of Eternity",
"country": "eeuu",
"year": 1955,
"author": "Isaac Asimov",
"created_at": "2021-07-15T14:47:25.262738",
"updated_at": "2021-07-15T14:50:47.697224"
}
Delete a book
curl -XDELETE https://example-of-crud-in-django-jrf.herokuapp.com/api/book/1/
Output
{
"id": null,
"title": "The End of Eternity",
"country": "eeuu",
"year": 1955,
"author": "Isaac Asimov",
"created_at": "2021-07-15T14:47:25.262738",
"updated_at": "2021-07-15T14:50:47.697224"
}
Check that the server is up
curl https://example-of-crud-in-django-jrf.herokuapp.com/ping/
Output
{"ping": "pong!"}
If you get lost at any step, don't hesitate to check out or download the code I have hosted in the GitHub repository.
Are you ready? Let's begin!
Activity 1
Create an API to register the runners of a fun run. The required data is up to you.
Activity 2
Create an API to search for or add TV shows you have watched. You must also be able to give them a rating and list them ordered by rating.
This work is under a Attribution-NonCommercial-NoDerivatives 4.0 International license.
Desafíos de programación atemporales y multiparadigmáticos
Te encuentras ante un librillo de actividades, divididas en 2 niveles de dificultad. Te enfrentarás a los casos más comunes que te puedes encontrar en pruebas técnicas o aprender conceptos elementales de programación.
Buy the bookWill you buy me a coffee?
This is how I keep writing without ads or paywalls.
Sure, it's on me!
Comments
There are no comments yet.