Setup a Django Project

Here are the steps to perform to setup a Django project:

  1. Install Django dependencies

  2. Create a project directory

  3. Use django-admin to start a new project

  4. Run and preview the project

Pre-requisites

Requires a Python project. Follow the Setup Python Project guide to complete this.

Guide code:

Install Django dependencies

Add Django dependencies to requirements.txt

-r docs/requirements.txt
django
djangorestframework
pytest
pytest-cov
pytest-django
python-dotenv

I prefer following the convention to order the dependency names alphabetically.

Install the dependencies:

pip install -r requirements.txt

Create a project directory

Create a directory for the project:

mkdir src
cd src

Use django-admin to start a new project

Make sure you are in /src/ folder and create new django project, using django-admin command:

django-admin startproject blogapi .

The project files and directories should be like the following:

├── src
│   ├── blogapi
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   ├── asgi.py
│   │   └── wsgi.py
│   └── manage.py
├── requirements.txt
├── README.md
└── LICENSE

I have not included the docs folder intentionally.

Run and preview the project

Migrate the project database:

python manage.py migrate

This will create a db.sqlite3 file in the project directory:

├── src
│   ├── db.sqlite3
│   ├── blogapi
│   │   ├── __init__.py
│   │   ├── settings.py
│   │   ├── urls.py
│   │   ├── asgi.py
│   │   └── wsgi.py
│   └── manage.py
├── requirements.txt
├── README.md
└── LICENSE

Start a Django development server:

python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).
August 04, 2024 - 12:00:26
Django version 5.0.7, using settings 'blogapi.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Open a web browser window and navigate to http://127.0.0.1:8000/:

Screenshot of the user interface