How to setup Django + Starter Template

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

But if you want to skip all these steps and download the starter template you can 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 by a name. Here you can replace VENV_NAME with any name of your choice

python3 -m venv ./VENV_NAME
source VENV_NAME/bin/activate

Installing Django

Run the following command to install django inside your venv

pip3 install django

Freeze the requirements to keep track of the installed packanges. pip3 freeze > requirements.txt The above command will create a requirements.txt file which will contain all the versions of the libraries installed.

My requirement.txt looks like this:

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

It was installed a couple of more packages while installing Django. It also states the version of Django installed which is 5.0.6.

Create a Django Project

To create a new project run the following command. Here I have given the name of my project as 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 will 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 succesfully 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, we have to add it to installed apps. So go to django_project/settings.py and find the INSTALLED_APP section in and add the name of the app. Here is how it should look like:

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

    # New Appp
    'django_app'
]

Create a View

Lets create a new view inside the app. For this, lets navigoate 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 new url

To checkout the view we just created we have to create a new url and attach it with the view. We have to link the urls from the project directory to app directory.

Lets 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)
]

And 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 setup the Django project.

Push your changes to a Git repository (optional)

If you are working alone or if you are working with a team. Its 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 you repo.

First lets create a .gitignore file to ingore the files you do not want to commit. For instance you definately do not want to push venv file to git so add the name of the venv 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 to 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.

So go ahead and use this command to clone django from git in an empty folder:

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

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

Whats Next

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

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

Thanks for reading!