A Beginner's Guide to Create Your First Django Project

A Beginner's Guide to Create Your First Django Project

3 simple steps to prepare your initial Django project

In this comprehensive blog post, I will provide a step-by-step guide on how to set up a new Django project from scratch. We will cover everything from installing the necessary dependencies and creating a virtual environment, to configuring the project settings and running the development server.

Before we begin, please create a parent directory to store all the files. In my case, I will name it myproject.

Now we can start.

The first step is to create a new virtual environment.

Install the virtualenv module:

sudo apt-get install virtualenv

Create a new environment:

cd myproject
virtualenv venv -p 3 # -p -> python version
source venv/bin/activate

After activating the environment, all modules will be installed within it.

Step 2: We will install Django and start a new project.

pip3 install django
django-admin startproject project

Step 3: Use environment variables and configure settings.py.

Install python-dotenv :

pip3 install python-dotenv

Now, create a .env file next to settings.py.

.
└── myproject/
    ├── venv/
    │   └── ...
    └── project/
        ├── project/
        │   ├── .env
        │   ├── asgi.py
        │   ├── __init__.py
        │   ├── settings.py
        │   ├── urls.py
        │   └── wsgi.py
        ├── manage.py
        ├── static/
        │   └── ...
        └── media/
            └── ...

Lets create some environment variables:

SECRET_KEY="Your_Secret_Key"
DEBUG=1
ALLOWED_HOSTS=127.0.0.1 # If allowed hostes more than 1 seprate them by ;
DEBLOYED=0
TRUSTED_ORIGINS="Your domians with http/https seperated by ;"
# "https://marwanfazora.com;https://blog.marwanfazora.com"

Now let's configure our settings.py to work both locally and during deployment based on the DEPLOYED variable.


# load environment variables
from dotenv import load_dotenv
import os
load_dotenv()

SECRET_KEY = os.getenv('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(int(os.getenv('DEBUG')))

ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS').split(';')


STATIC_URL = 'static/'
MEDIA_URL = 'media/'
MEDIA_ROOT = 'media/'

if bool(int(os.getenv('DEBLOYED'))):
    STATIC_ROOT = 'static/'
    CSRF_TRUSTED_ORIGINS = os.getenv('TRUSTED_ORIGINS').split(';')
else:
    STATICFILES_DIRS = [
        BASE_DIR / "static",
    ]

Now, configure media and static in urls.py.

from django.conf import settings
from django.conf.urls.static import static

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Yay! We've successfully completed this step! 😄

Let's run our project!

python3 manage.py runserver

And there you have it! Your first project is up and running, and you can run it both locally and on deployment. Great job, and congratulations! 🎉