How to Setup Django + Starter Template

In this article, I'll show you how to set up a Django project following best practices.

If you want to skip these steps and download the starter template, click here to get the Django Starter Template.

If you want to know how to set it up yourself, read on...

Setting up a Virtual Environment

Create a virtual environment. Replace VENV_NAME with any name of your choice:

python3 -m venv ./VENV_NAME

Activate the virtual environment:

source VENV_NAME/bin/activate

Installing Django

Run the following command to install Django inside your virtual environment:

pip3 install django

Freeze the requirements to keep track of the installed packages:

pip3 freeze > requirements.txt

The above command will create a requirements.txt file containing all the versions of the installed libraries. My requirements.txt looks like this:

asgiref==3.8.1
Django==5.0.6
sqlparse==0.5.0

Create a Django Project

To create a new project, run the following command. Here, I have named my project django_project:

django-admin startproject django_project .

The command creates the following files and folders:

django_project
│   __init__.py
│   asgi.py    
│   settings.py  
│   urls.py  
│   wsgi.py  
VENV_NAME
|...
manage.py
requirements.txt

To check if everything is working fine, run the following command:

python3 manage.py runserver

You will get something like this:

Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 24, 2024 - 12:00:42
Django version 5.0.6, using settings 'django_project.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

This means that Django is successfully running on http://127.0.0.1:8000/.

You are all set!

Create a Django App

python3 manage.py startapp django_app

After creating the app, add it to installed apps. Go to django_project/settings.py, find the INSTALLED_APPS section, and add the name of the app. It should look like this:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # New App
    'django_app'
]

Create a View

Let's create a new view inside the app. Navigate to django_app/views.py and create an index function that returns an HTTP Response "Hello World!". Paste the code below in views.py:

from django.shortcuts import render
from django.http import HttpResponse

# Create your views here.
def index(request):
    return HttpResponse("Hello World!")

Add a New URL

To check out the view we just created, we have to create a new URL and attach it to the view. We have to link the URLs from the project directory to the app directory.

Create a new urls.py file inside the django_app directory and paste the following code:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.index)
]

Add the following to django_project/urls.py:

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

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

Now you can start the server again and visit http://127.0.0.1:8000/ to view the HTTP Response: "Hello World!"

With this, you have successfully set up the Django project.

Push Your Changes to a Git Repository (Optional)

If you are working alone or with a team, it's always best to push your code to a Git repository. In the following steps, I'll show you how to push the current code to your repo.

First, create a .gitignore file to ignore the files you do not want to commit. For instance, you definitely do not want to push the virtual environment folder to Git, so add the name of the virtual environment folder to the .gitignore file:

VENV_NAME

For now, this is fine, but if you have files that you want to ignore, you can go here to add more types of files: GitHub Default .gitignore File.


Django Starter Template

Tired of setting up Django for every project?

In this post, I'll give you a template to quickly clone from Git and get started. It follows all the best practices, so you won't have to worry about keeping that.

Use this command to clone Django from Git in an empty folder:

git clone https://github.com/ShilpThapak/Django-Starter-Template.git .

Now you just have to activate your virtual environment and run pip install -r requirements.txt, and you are all set.

What's Next

After setting up Django, your backend settings are all set. Now you can decide whether you want to go with JavaScript and Django's Jinja Template or integrate React with the Django backend.

In the next post, I'll cover both these approaches.

Thanks for reading!