Welcome to this tutorial on creating a Django REST API in just 30 minutes! Django is a high-level Python web framework that makes it easy to build web applications quickly and efficiently. In this tutorial, we will focus on creating a REST API using Django’s built-in capabilities.
-
Install Django
The first step is to install Django if you haven’t already. You can do this by running the following command in your terminal:pip install django
-
Create a new Django project
Next, create a new Django project by running the following command in your terminal:django-admin startproject myapi
This will create a new directory called
myapi
with the necessary project files. -
Create a Django app
Navigate into themyapi
directory and create a new Django app by running the following command:python manage.py startapp myapp
- Define models
Open themodels.py
file in themyapp
directory and define your models. For this tutorial, let’s create a simple model for aProduct
:from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
price = models.FloatField()
5. Create migrations
Generate migrations for your models by running the following commands in your terminal:
python manage.py makemigrations
python manage.py migrate
6. Create serializers
Next, create serializers for your models in a new file called `serializers.py` in the `myapp` directory:
```python
from rest_framework import serializers
from .models import Product
class ProductSerializer(serializers.ModelSerializer):
class Meta:
model = Product
fields = '__all__'
- Create views
Create views for your API by defining viewsets in a new file calledviews.py
in themyapp
directory:from rest_framework import viewsets from .models import Product from .serializers import ProductSerializer
class ProductViewSet(viewsets.ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductSerializer
8. Register views
Register your views in the `urls.py` file in the `myapp` directory:
```python
from rest_framework import routers
from .views import ProductViewSet
router = routers.DefaultRouter()
router.register(r'products', ProductViewSet)
urlpatterns = router.urls
- Update the main
urls.py
file
Include your app’s URLs in the mainurls.py
file in themyapi
directory:from django.urls import path, include
urlpatterns = [
path(‘api/’, include(‘myapp.urls’)),
]
10. Run the server
Start the Django server by running the following command in your terminal:
python manage.py runserver
11. Test the API
You can now test your API by navigating to `http://127.0.0.1:8000/api/products` in your web browser. You should see a list of products if everything is set up correctly.
Congratulations! You have successfully created a Django REST API in just 30 minutes. This tutorial covers the basics of creating a simple API using Django's built-in features. There is a lot more you can do with Django and the Django REST framework, so make sure to explore the documentation for more advanced features and customization options. Happy coding!
Do more video about django framework.
What an easy explanation. Absolutely clear and concise❤
we need more videos on react and django
❤
Great tutorial!
Want full crash course here bro. Loved it.
Are you from india???
Hey Pedro, it would be AWESOME if you could do a video with Django Rest (backend) + Nextjs (frontend) with maybe using tools like redux toolkit, rtk query, JWT authentication and how they interact with one another. Cheers!
Exelent 5/5
Very good tutorial. Please more
thanks man
Thanks for this video really to very good
Is the code available?
Thanks Pedro, great video.
Hi, thank you for the content. Please continue to make more and build full stack applications which will help us understand how everything works together. More Django + React JS tutorials.
Yes,great indeed. Please do a python with the Rest Framework and let's use React as front-end and deploy it to a shared server.Let use Mysql as Database instead of Sqlite
awesome overview of django for REST APIs! I needed to do a quick recap for an interview and this video was very comprehensive. Maybe a cool follow up would be to see how to interact with your django API using Postman? Or what that workflow would look like. Keep making great tutorials like this!
You are a natural. So eloquent and easy to follow. Please keep making videos such as this. I’d love to see a concise video like this that connects a frontend like Svelte to DRF. Thank you again!
Easy, concise but detailed. Good job man
Man you’re low key our Superman 😄