Django Create App
Building modular components in Django
๐ฆ What is a Django App?
A Django app is a self-contained module that performs a specific function within your project. Apps are reusable components like blogs, user authentication, or shopping carts that can be plugged into any Django project.
# Create a new app
python manage.py startapp blog
Key App Components
Models
Define your data structure
class Post(models.Model):
title = models.CharField()
Views
Handle request logic
def home(request):
return render(request, 'home.html')
Templates
Display HTML to users
<h1>{{ title }}</h1>
Tests
Test your app functionality
class PostTestCase(TestCase):
def test_post(self):
๐น Creating Your First App
Apps are created using the manage.py utility. Each app should have a single, clear purpose to maintain modularity and reusability.
# Navigate to your project directory
cd mysite
# Create a new app called 'blog'
python manage.py startapp blog
# Create another app called 'users'
python manage.py startapp users
Result:
โ App 'blog' created successfully
โ App 'users' created successfully
๐น App Structure
When you create an app, Django generates several files automatically:
blog/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
migrations/
__init__.py
- models.py: Define database models
- views.py: Handle request/response logic
- admin.py: Register models for admin interface
- tests.py: Write unit tests
- migrations/: Database schema changes
๐น Registering Your App
After creating an app, you must register it in your project's settings.py file to activate it. Add the app name to the INSTALLED_APPS list.
# settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Your apps
'blog',
'users',
]
๐ก Important:
Always register your app in INSTALLED_APPS, or Django won't recognize it!
๐น Creating a Simple Model
Models define the structure of your database tables. Here's a basic example of a blog post model:
# blog/models.py
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
# Create database tables
python manage.py makemigrations
python manage.py migrate
๐น Creating a Simple View
Views handle the logic for processing requests and returning responses. Here's a basic view that displays a list of blog posts:
# blog/views.py
from django.shortcuts import render
from .models import Post
def post_list(request):
posts = Post.objects.all()
return render(request, 'blog/post_list.html', {'posts': posts})
def home(request):
return render(request, 'blog/home.html')
๐น App vs Project
Understanding the difference between apps and projects is crucial for organizing your Django code effectively:
๐ Project:
- Container for your entire website
- Contains settings, URLs, and configurations
- Can contain multiple apps
- Example: An e-commerce website
๐ฆ App:
- Performs a specific function
- Reusable across projects
- Self-contained module
- Example: Blog, shopping cart, user profiles