Learn Django REST API in just 30 minutes with this Django tutorial

Posted by


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.

  1. 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
  2. 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.

  3. Create a Django app
    Navigate into the myapi directory and create a new Django app by running the following command:

    python manage.py startapp myapp
  4. Define models
    Open the models.py file in the myapp directory and define your models. For this tutorial, let’s create a simple model for a Product:

    
    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__'
  1. Create views
    Create views for your API by defining viewsets in a new file called views.py in the myapp 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
  1. Update the main urls.py file
    Include your app’s URLs in the main urls.py file in the myapi 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!
0 0 votes
Article Rating

Leave a Reply

34 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@kameshethiraj4130
2 hours ago

Do more video about django framework.

@kameshethiraj4130
2 hours ago

What an easy explanation. Absolutely clear and concise❤

@hrithik3204
2 hours ago

we need more videos on react and django

@obiajulumbanefo3545
2 hours ago

@loveafinni
2 hours ago

Great tutorial!

@nishatzayn7682
2 hours ago

Want full crash course here bro. Loved it.

@alexuniquone138
2 hours ago

Are you from india???

@joaoarthurbandeira
2 hours ago

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!

@ShabbiHb
2 hours ago

Exelent 5/5

@krzysiekkrzysiek9059
2 hours ago

Very good tutorial. Please more

@mikemusengejr
2 hours ago

thanks man

@ahmadsaleh3451
2 hours ago

Thanks for this video really to very good

@arnaldojimenez4769
2 hours ago

Is the code available?

@arnaldojimenez4769
2 hours ago

Thanks Pedro, great video.

@katlegolionel1174
2 hours ago

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.

@davidmukoro1166
2 hours ago

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

@JoeceOfficial
2 hours ago

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!

@shahabahmed6991
2 hours ago

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!

@sdvogo1520
2 hours ago

Easy, concise but detailed. Good job man

@stackstudiossl
2 hours ago

Man you’re low key our Superman 😄

34
0
Would love your thoughts, please comment.x
()
x