2. Django REST API: Project Setup
Django REST API – Project Setup

In this post, we will set up the base structure for our Django REST API project.
We will create a Django project, install Django REST Framework, create our first app, configure the settings, and prepare the project for building API endpoints.



 

Activating your virtual environment
Before installing Django and Django REST Framework, make sure your virtual environment is activated.

activate myfirstenv

If you are using Miniconda, this should activate the environment where your project dependencies will be installed.

 

Installing Django and Django REST Framework
Now we need to install Django and Django REST Framework.

pip install django djangorestframework

Django will be used as the main web framework, while Django REST Framework will provide the tools needed to build our API.

 

Creating the Django project
Next, create a new Django project.

django-admin startproject config

 

Your project should now have a structure similar to this:

config/
    config/
        __init__.py
        asgi.py
        settings.py
        urls.py
        wsgi.py
    manage.py

 

Creating the API app
Django apps help keep our code organized into smaller and reusable parts.

For this series, we will create an app called projects.

python manage.py startapp projects

After running this command, Django will create a new folder with the basic files for the app.

projects/
    __init__.py
    admin.py
    apps.py
    migrations/
    models.py
    tests.py
    views.py

 

Adding apps to settings.py
Newly created apps must be added to the INSTALLED_APPS list.

We also need to add rest_framework, which enables Django REST Framework in our project.

INSTALLED_APPS = [
    ...

    'rest_framework',
    'projects',
]

At this point, Django knows that both Django REST Framework and our projects app are part of the project.

 

Running initial migrations
Before running the server, we should apply Django’s initial database migrations.

These migrations create the default database tables used by Django, such as authentication, admin, sessions, and content types.

python manage.py migrate

By default, Django uses SQLite, which is enough for development and for learning the basics of Django REST Framework.

 

Creating a superuser
A superuser gives us access to the Django Admin Panel.

python manage.py createsuperuser

Follow the instructions in the terminal and create your admin account.

 

Running the development server
Now we can start the local development server.

python manage.py runserver

If everything is working correctly, Django should start the server at:

http://127.0.0.1:8000/

 

Access through the browser
You can now access the project through your browser.

Root: http://localhost:8000/
Admin: http://localhost:8000/admin/

At this stage, we do not have API endpoints yet, but the base project is ready.

 

Creating an API urls.py file
To keep our API routes organized, we can create a urls.py file inside the projects app.

projects/
    __init__.py
    admin.py
    apps.py
    migrations/
    models.py
    tests.py
    urls.py
    views.py

Inside config/projects/urls.py, add the following:

from django.urls import path

urlpatterns = [
    
]

For now, this file will remain empty. We will add our API endpoints in the next posts.

 

Including app URLs in the main project
Now we need to connect the projects app URLs to the main project URLs.

Open config/config/urls.py and update it like this:

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('projects.urls')),
]

This means that every URL from the projects app will start with /api/.

http://localhost:8000/api/

 

Recommended project structure
At the end of this setup, your project should look similar to this:

config/
    config/
        __init__.py
        asgi.py
        settings.py
        urls.py
        wsgi.py

    projects/
        __init__.py
        admin.py
        apps.py
        migrations/
        models.py
        tests.py
        urls.py
        views.py

    manage.py

This gives us a clean starting point for building our REST API.

 

What comes next?
In the next post, we will start building the core structure of our API.

We will create our first models, transform them into JSON using serializers, and expose them through API views.

Django REST API: Models, Serializers and Views