Creating a Django Project: A Step-by-Step Guide

Last updated 4 months, 3 weeks ago | 357 views 75     5

Tags:- Python Django

Django is a high-level Python web framework that encourages rapid development and clean, pragmatic design. One of the first steps in using Django is creating a new project — a container for your web applications.

This guide walks you through the process of creating a Django project, from installation to project structure, and includes practical tips and common mistakes to avoid.


Prerequisites

Before you start, make sure you have the following installed:

  • Python 3.8 or higher

  • pip (Python package installer)

You can check versions with:

python --version
pip --version

Step 1: Set Up a Virtual Environment

Using a virtual environment is best practice as it isolates your project’s dependencies.

Create a virtual environment:

python -m venv venv

Activate the virtual environment:

  • macOS/Linux:

source venv/bin/activate
  • Windows:

venv\Scripts\activate

You’ll know it's activated when your terminal prompt shows (venv).


Step 2: Install Django

With your virtual environment activated, install Django using pip:

pip install django

Verify installation:

django-admin --version

Step 3: Create a Django Project

Use the django-admin command to create a new project.

django-admin startproject mysite
cd mysite

This creates a directory structure like:

mysite/
├── manage.py
└── mysite/
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    ├── asgi.py
    └── wsgi.py

What these files mean:

File Purpose
manage.py Command-line utility for running tasks
settings.py Main configuration for the project
urls.py Maps URLs to views
wsgi.py Entry point for WSGI servers
asgi.py Entry point for ASGI servers

Step 4: Run the Development Server

To see if everything is working, run:

python manage.py runserver

Visit http://127.0.0.1:8000/ in your browser. You should see Django’s welcome page!


Step 5: Create an App

Django projects consist of one or more apps, each responsible for a specific function (e.g., blog, authentication, API).

To create an app:

python manage.py startapp blog

This creates:

blog/
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── views.py
└── migrations/

Step 6: Register the App

Open mysite/settings.py, and add your app to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'blog',
]

This tells Django to include your app in the project.


⚙️ Step 7: Configure URLs

Create urls.py in your app folder (blog/urls.py):

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]

Define the home view in blog/views.py:

from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello from the Blog!")

Link app URLs in mysite/urls.py:

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

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

Step 8: Set Up the Database

By default, Django uses SQLite, which is perfect for development.

Run the following commands to create the initial database:

python manage.py makemigrations
python manage.py migrate

Step 9: Create a Superuser

To access the admin panel:

python manage.py createsuperuser

Provide a username, email, and password when prompted.

Run the server and visit http://127.0.0.1:8000/admin to log in.


Project Created!

You now have a working Django project with one app (blog) that responds to requests and integrates with the admin panel.


✅ Best Practices for Django Projects

  • ✅ Always use a virtual environment.

  • ✅ Organize settings into separate files for development/production.

  • ✅ Keep secret keys and sensitive data in environment variables.

  • ✅ Use version control (e.g., Git) to manage your project code.

  • ✅ Keep apps modular—each app should do one thing well.


⚠️ Common Pitfalls to Avoid

Mistake Solution
❌ Forgetting to activate the virtual environment ✅ Run source venv/bin/activate before commands
❌ Not adding the app to INSTALLED_APPS ✅ Add your app name in settings.py
❌ Skipping migrations ✅ Always run makemigrations and migrate
❌ Hardcoding sensitive data in settings ✅ Use environment variables or python-decouple
❌ Modifying manage.py ✅ Don’t edit unless you know what you’re doing

Conclusion

Creating a Django project is the first step toward building powerful web applications with Python. By following these steps — from setting up a virtual environment to configuring apps and URLs — you're now ready to build and scale your Django app.