DEV Community

Gopal Ghate
Gopal Ghate

Posted on

Django Project & App Structure Explained

Django is built with a clear separation of concerns, which makes your code easier to maintain. In this article, we’ll go deeper into projects, apps, models, views, URLs, templates, and migrations with a fully working example. By the end, you will have a running project with a correctly rendered template.

Django Project vs App

Project → The overall Django setup, holding settings, URL routing, and configurations.

App → A module inside your project designed for a specific purpose. You can have multiple apps inside one project.
For example

mysite/ # Project
├── manage.py
├── mysite/ # Project settings folder
└── blog/ # App folder
Enter fullscreen mode Exit fullscreen mode

You might have multiple apps like users, payments, or products under the same project.

Project Structure Explained

When you run django-admin startproject mysite, you get:

mysite/
├── manage.py
└── mysite/
├── __init__.py
├── asgi.py
├── settings.py
├── urls.py
└── wsgi.py
Enter fullscreen mode Exit fullscreen mode

Let’s break these files down:

manage.py → Command-line tool to run server, create apps, apply migrations, open Django shell, etc.

settings.py → Central configuration (DB, installed apps, middleware, security keys).

urls.py → Main URL router; maps paths to views.

wsgi.py / asgi.py → Entry point for deployment (used by production servers).

Creating Your First App

Run :

python manage.py startapp blog #(blog -> app name, you can add your app name here)
Enter fullscreen mode Exit fullscreen mode

This creates following folder structure

blog/
├── admin.py
├── apps.py
├── migrations/
├── models.py
├── tests.py
├── views.py
Enter fullscreen mode Exit fullscreen mode

Add newly created app to INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog', # add your app name here.
]
Enter fullscreen mode Exit fullscreen mode

Adding Views & URLs

In blog/views.py

from django.shortcuts import render

def home(request):
return render(request, 'blog/home.html')
Enter fullscreen mode Exit fullscreen mode

Create blog/urls.py

from django.urls import path
from . import views


urlpatterns = [
path('', views.home, name='home'),
]
Enter fullscreen mode Exit fullscreen mode

Include app urls in project urls (mysite/urls)

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


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

Templates Setup

Folder Structure (App-Level Templates)

blog/
├── templates/
  │ └── blog/
      │ └── home.html
Enter fullscreen mode Exit fullscreen mode

home.html example:

<!DOCTYPE html>
<html>
<head>
<title>My First Django Page</title>
</head>
<body>
<h1>Welcome to My Blog!</h1>
<p>This page is powered by Django templates.</p>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

settings.py Configuration:

Make sure APP_DIRS=True:

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [], # leave empty for app-level templates
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
           'django.template.context_processors.debug',
           'django.template.context_processors.request',
           'django.contrib.auth.context_processors.auth',
           'django.contrib.messages.context_processors.messages',
        ],
     },
  },
]
Enter fullscreen mode Exit fullscreen mode

Now render(request, 'blog/home.html') will find the template

Migrations & Database

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

Run the server

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Summary

In this article, we:

  • Understood the difference between projects and apps.

  • Learned about project files and what they do.

  • Created an app and added views, urls, and templates.

  • Correctly configured settings.py and rendered a template.

  • Ran migrations and started the server.

Top comments (1)

Collapse
 
prime_1 profile image
Roshan Sharma

Nice post, you did a good job explaining Django’s project/app structure in a way that doesn’t confuse newbies.

Just wondering: when do you decide to split functionality into a new app vs keeping it inside the same app?