DEV Community

Cover image for Django Test Driven Social Media REST API - Getting Started
Suyash K.
Suyash K.

Posted on • Updated on

Django Test Driven Social Media REST API - Getting Started

Basic Project Setup

mkdir DjangoRESTAPI && cd $_
python3 -m venv .venv
. .venv/bin/activate
pip install django djangorestframework
pip freeze > requirements.txt
git init
curl https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore > .gitignore
django-admin startproject config .
Enter fullscreen mode Exit fullscreen mode

This should set up a boilerplate project with project configs and initialized git repository.
The Project Structure should resemble the following.

$ tree -a --gitignore -I ".git/"
.
├── .gitignore
├── config
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── manage.py
└── requirements.txt
Enter fullscreen mode Exit fullscreen mode
$ cat requirements.txt 
asgiref==3.6.0
backports.zoneinfo==0.2.1
Django==4.1.6
djangorestframework==3.14.0
pytz==2022.7.1
sqlparse==0.4.3
Enter fullscreen mode Exit fullscreen mode

Initial Commit: Getting Started

mkdir -p src/user
touch src/__init__.py
python3 manage.py startapp user src/user
Enter fullscreen mode Exit fullscreen mode

The Project Structure should resemble the following:

$ tree src/
src/
├── __init__.py
└── user
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py
Enter fullscreen mode Exit fullscreen mode

Now, to add the user app to the project, edit src/user/apps.py

from django.apps import AppConfig

class UserConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'src.user'
Enter fullscreen mode Exit fullscreen mode

and then edit config/settings.py

# ...

INSTALLED_APPS = [
    'django.contrib.admin',
    # ...
    'src.user'
]
# ...
Enter fullscreen mode Exit fullscreen mode

Now we can run migrations and start the development server.

python3 manage.py makemigrations
python3 manage.py migrate
python3 manage.py runserver
Enter fullscreen mode Exit fullscreen mode

This should start the development server on localhost. Now we can open the project in the code editor. Also we can commit our changes to the git repository.

Top comments (0)