Table of content

Prerequisites

Step 1: Set Up a Django Project

Step 2: Create a Django App

Step 3: Define Models

Step 4: Create Views and URLs

Step 5: Create Templates

Step 6: Run the Application

Conclusion

Building Your First Python Django App

October 26, 2024

Django is a powerful, high-level Python web framework that encourages rapid development and clean, pragmatic design. It’s widely used by developers to create secure, scalable applications in a fraction of the time. In this guide, we’ll walk through setting up your first Django app, creating a model, and rendering data through views and templates.


Prerequisites

Before we start, make sure you have:

  1. Python (version 3.6 or higher)
  2. PIP - Python's package installer
  3. Virtualenv (optional but recommended for managing dependencies)

Install Django by running:

pip install django

Step 1: Set Up a Django Project

Begin by creating a new Django project:

django-admin startproject myproject
cd myproject

The project will include several key files:

  • manage.py: Command-line utility to interact with the project.
  • settings.py: Contains configuration for the project, such as database settings and installed apps.

Start the development server to check if the setup is successful:

python manage.py runserver

Navigate to http://127.0.0.1:8000 to see the default Django welcome page.


Step 2: Create a Django App

In Django, an “app” is a web application that performs a specific function. Create an app called blog:

python manage.py startapp blog

Now, add the blog app to the INSTALLED_APPS section of settings.py:

# myproject/settings.py
INSTALLED_APPS = [
    ...
    'blog',
]

Step 3: Define Models

Models represent the database structure. Let’s create a simple blog model in blog/models.py:

# blog/models.py
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=200)
    content = models.TextField()
    published_date = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title

Run migrations to apply this model to the database:

python manage.py makemigrations
python manage.py migrate

Step 4: Create Views and URLs

A view is where we define what data to show and how to display it. Create a simple view to display blog posts in blog/views.py:

# 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})

Then, set up a URL route in blog/urls.py to connect the view to a specific path:

# blog/urls.py
from django.urls import path
from . import views

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

Finally, include the blog’s URLs in the project’s main urls.py:

# myproject/urls.py
from django.contrib import admin
from django.urls import path, include

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

Step 5: Create Templates

Templates define the HTML structure for your views. Create an templates folder in the blog app, then add a post_list.html file:

<!-- blog/templates/blog/post_list.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Blog</title>
</head>
<body>
    <h1>Blog Posts</h1>
    {% for post in posts %}
        <h2>{{ post.title }}</h2>
        <p>{{ post.content }}</p>
        <small>{{ post.published_date }}</small>
    {% endfor %}
</body>
</html>

Step 6: Run the Application

Now, start the Django development server:

python manage.py runserver

Visit http://127.0.0.1:8000 to view your blog. You should see your blog posts displayed according to the template we created.

Conclusion

Congratulations! You've created your first Django app with a simple blog model, view, and template. This is just the beginning of what you can achieve with Django. You can add more features, like user authentication, comments, or a search function, as you grow your app.

For further learning, check out the official Django documentation and dive into Django's many built-in features to enhance your project.

Happy coding! 🥷