π Professional Django Project Setup Guide (with PostgreSQL, .env, Git, and venv)

Table of contents
- π§ Tools & Requirements
- π 2. Set Up Python Virtual Environment
- π¦ 3. Install Django and Dependencies
- πΏ What is environs?
- βοΈ 4. Start Django Project
- π The Project Structure Will Look Like:
- π 5. Set Up .env and Use environs
- π 6. Push Project to GitHub
- π§ͺ 7. Run Migrations & Test Project
- π§© 8. Add Home Page to Confirm Setup
- π’ 9. Other Database Notes
- β 10. Final Testing
- π Summary Notes
- πBonus: Add a Beautiful Template Page
- You should see:
- π― You're Ready!
- π Final Project Structure

I'm Mahfuz Hossain, a third-year IT student from Bangladesh, passionate about backend development.πThis guide walks you through the steps to set up a professional Django project using industry best practices. It covers everything from project initialization, setting up Python virtual environments, managing secrets with .env
, using environs
for configuration, Git version control, and connecting PostgreSQL.
By following this guide, you can create a scalable, secure, and maintainable Django project - perfect for production and portfolio use.
π§ Tools & Requirements
π 1. Project Initialization
Create Project Directory
First, you need to create a dedicated directory for your Django project. This helps keep everything organized. Letβs start by creating the project folder and initializing Git.
mkdir myproject
cd myproject
git init
β
Create a .gitignore
File
This file tells Git which files/folders to ignoreβsuch as the virtual environment, .env
file, and compiled files:
touch .gitignore
Paste the following content into .gitignore
:
__pycache__/
*.py[cod]
*.sqlite3
db.sqlite3
*.log
.venv/
.env
/staticfiles
π 2. Set Up Python Virtual Environment
π§ͺ What is a virtual environment?
A virtual environment is a self-contained Python workspace that isolates your projectβs dependencies. This prevents conflicts between projects.
β How to Set It Up:
python3 -m venv .venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
Why use virtualenv?
Avoids version conflicts between projects.
Keeps your project lightweight and reproducible.
Industry-standard practice for Python apps.
π¦ 3. Install Django and Dependencies
β For PostgreSQL Projects:
pip install django environs psycopg2-binary
django
: Core web framework.environs
: Simplifies working with environment variables.psycopg2-binary
: PostgreSQL driver.
β For SQLite (Default):
pip install django environs
πΏ What is environs
?
environs
is a lightweight Python library that helps you read and validate environment variables easily and safely from a .env
file.
β Save Dependencies
pip freeze > requirements.txt
This creates a list of all installed packages so others can install the exact same environment with:
pip install -r requirements.txt
βοΈ 4. Start Django Project
Create the main Django project structure.
django-admin startproject myproject .
The .
ensures it doesn't nest the project in an extra folder.
π The Project Structure Will Look Like:
myproject/
βββ .git/
βββ .gitignore
βββ .env
βββ .venv/
βββ myproject/
β βββ __init__.py
β βββ asgi.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ manage.py
βββ requirements.txt
π 5. Set Up .env
and Use environs
β
Create .env
File
DEBUG=True
SECRET_KEY=your-secret-key
DATABASE_NAME=yourdbname
DATABASE_USER=yourdbuser
DATABASE_PASSWORD=yourdbpassword
DATABASE_HOST=localhost
DATABASE_PORT=5432 # for mysql 3306
π Why is .env
important?
β Keeps sensitive data out of your code
You donβt want your secret keys, passwords, or API keys visible in settings.py
. Instead, you store them in .env
and load them safely using libraries like environ
β
Modify settings.py
to Load .env
Variables
Edit config/
settings.py
:
from environs import Env
env = Env()
env.read_env()
SECRET_KEY = env.str("SECRET_KEY")
DEBUG = env.bool("DEBUG", default=False)
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': env.str("DATABASE_NAME"),
'USER': env.str("DATABASE_USER"),
'PASSWORD': env.str("DATABASE_PASSWORD"),
'HOST': env.str("DATABASE_HOST"),
'PORT': env.str("DATABASE_PORT"),
}
}
For SQLite, you can skip database env variables and use:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
π 6. Push Project to GitHub
β Version Control Commands
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/yourusername/myproject.git
git push -u origin main
Why GitHub?
Share code with others
Maintain version history
Easily deploy from GitHub on platforms like Heroku, Vercel, or Render
π§ͺ 7. Run Migrations & Test Project
Migrations sync your models with the database.
python manage.py migrate
python manage.py runserver
Visit http://127.0.0.1:8000 β You should see the Django welcome page.
π§© 8. Add Home Page to Confirm Setup
β Create a Django App
python manage.py startapp myapp
Add 'myapp'
to INSTALLED_APPS
in settings.py
.
β Add a Simple View
myapp/views.py:
from django.http import HttpResponse
def home(request):
return HttpResponse("<h1> Welcome to the Professional Django Setup! </h1>")
β Create App URL and Hook It In
myapp/urls.py:
from django.urls import path
from .views import home
urlpatterns = [
path('', home, name='home'),
]
myproject/urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
π’ 9. Other Database Notes
SQLite: Default, no setup needed.
PostgreSQL: Preferred in production.
MySQL:
pip install mysqlclient
'django.db.backends.mysql'
β 10. Final Testing
Run the server and visit http://127.0.0.1:8000
You should see:
This confirms your environment is ready! π―
π Summary Notes
Use
.venv
to isolate Python packages.Use
.env
for secrets andenvirons
to safely load them.Use PostgreSQL for serious projects.
Use
git
andGitHub
to track changes and collaborate.Create a homepage to verify setup works.
πBonus: Add a Beautiful Template Page
Replace the homepage with a rendered template:
myapp/
views.py
from django.shortcuts import render
def home(request):
return render(request, 'myapp/home.html')
myapp/templates/myapp/home.html
Create folders if needed. Then paste:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Django Project Ready</title>
<style>
body {
font-family: 'Segoe UI', sans-serif;
background-color: #f4f4f4;
text-align: center;
margin-top: 15%;
}
h1 { color: #2c3e50; font-size: 3em; }
p { color: #34495e; font-size: 1.3em; }
</style>
</head>
<body>
<h1>π Django Setup Complete!</h1>
<p>Your project is professionally configured and ready to build.</p>
</body>
</html>
Make Sure Template Path Works in settings.py
:
Django will auto-discover templates if the folder structure is correct:
myapp/
βββ templates/
β βββ myapp/
β βββ home.html
You should see:
Again run the server and visit http://127.0.0.1:8000
π― You're Ready!
You now have a clean, secure, and production-ready Django setup
π Final Project Structure
Hereβs what your project should now look like:
myproject/
βββ .git/ # Git repository
βββ .gitignore # Git ignore rules
βββ .env # Environment variables (NOT in Git)
βββ .venv/ # Virtual environment folder (NOT in Git)
βββ myproject/ # Main Django project directory
β βββ __init__.py
β βββ asgi.py
β βββ settings.py # Modified to use `environs`
β βββ urls.py # Includes `myapp.urls`
β βββ wsgi.py
βββ manage.py # Django's CLI entry point
βββ myapp/ # Django app folder
β βββ __init__.py
β βββ admin.py
β βββ apps.py
β βββ migrations/
β β βββ __init__.py
β βββ models.py
β βββ tests.py
β βββ urls.py # Created for app routing
β βββ views.py # Contains the home view
β βββ templates/
β βββ myapp/
β βββ home.html # Your amazing homepage
βββ requirements.txt # (Optional but recommended)
π Thank you for reading!
If you found this guide helpful, please consider following my blog for more developer-focused content.
Feel free to share your thoughts, questions, or suggestions in the comments as your feedback means a lot! π»
Subscribe to my newsletter
Read articles from Mahfuz Hossain directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
