Build Your First Django App
This guide will walk you through creating a simple Django application from scratch. You’ll create a blog with posts that can be viewed and managed through Django’s admin interface.Make sure you have Django installed before starting this guide.
Create a Django Project
Start by creating a new Django project. This sets up the basic structure and configuration:This creates a project directory with the following structure:
Create a Django App
Django projects are composed of apps. Create a blog app:This creates a Now register the app in
blog/ directory with the following structure:mysite/settings.py:mysite/settings.py
Define a Model
Models define your database structure. Create a
Post model in blog/models.py:blog/models.py
This model uses patterns from Django’s source code:
verbose_namefor human-readable names (django/contrib/auth/models.py:39)gettext_lazyfor internationalization support (django/contrib/auth/models.py:13)db_indexfor query optimization (django/contrib/flatpages/models.py:9)Metaclass for model options (django/contrib/flatpages/models.py:31)
Create and Run Migrations
Django uses migrations to apply model changes to your database:You should see output like:
Create Views and URL Patterns
Create views to display your blog posts. Update Create a URL configuration in Include the blog URLs in your main
blog/views.py:blog/views.py
blog/urls.py:blog/urls.py
mysite/urls.py:mysite/urls.py
These views use Django’s built-in generic views:
ListViewfor displaying multiple objects (django/views/generic/list.py:9)DetailViewfor displaying a single object (django/views/generic/detail.py)get_object_or_404shortcut (django/shortcuts.py:70)
Register Model in Admin
Make your model editable in Django’s admin interface. Update Create a superuser to access the admin:Follow the prompts to set username, email, and password.
blog/admin.py:blog/admin.py
Create Templates
Create template files for your views. First, create a templates directory:Create
blog/templates/blog/post_list.html:blog/templates/blog/post_list.html
Test Your Application
Open your browser and visit:
- Admin interface: http://127.0.0.1:8000/admin/
- Log in with your superuser credentials
- Click “Posts” to add blog posts
- Blog posts list: http://127.0.0.1:8000/blog/
- View all your published posts
- Individual post: http://127.0.0.1:8000/blog/post/1/
- View a specific post (after creating one in admin)
The development server automatically reloads when you make code changes. No need to restart it manually!
What You’ve Built
Congratulations! You’ve created a Django application with:- Data models defining your database schema
- URL routing mapping URLs to views
- Views handling requests and returning responses
- Templates rendering HTML with Django’s template language
- Admin interface for content management
- Database migrations for schema versioning
Next Steps
Models & ORM
Deep dive into Django’s powerful Object-Relational Mapper
Views & Templates
Learn about function-based and class-based views
Forms
Handle user input with Django’s form system
Authentication
Add user registration and login to your app
Code Examples From Django Source
The code in this guide uses real patterns from Django’s source:- Model definition: Based on
PermissionandGroupmodels (django/contrib/auth/models.py:39-138) - Generic views:
ListViewandDetailView(django/views/generic/list.py, django/views/generic/detail.py) - URL patterns: Using
path()andinclude()(django/urls/conf.py) - Shortcuts:
render()andget_object_or_404()(django/shortcuts.py:19-98) - Field types:
CharField,TextField,DateTimeField(django/db/models/fields/)