DEV Community

Azmol Hossain
Azmol Hossain

Posted on

How to Create a Django App for Beginners

If you're just getting started with Django, this guide will walk you
step-by-step through creating your first project and app. By the end,
you'll understand how to set up Django, create models, and run your
project in your browser. Let's dive in!


πŸ›  Step 1: Project Setup

Before creating a Django project, we need to set up our environment. A
great tool for managing Python dependencies is Pipenv.

Open your terminal and run:

pip install pipenv   # install pipenv (one-time setup)
pipenv shell         # activate virtual environment
pipenv install django  # install Django
Enter fullscreen mode Exit fullscreen mode

This ensures that your project has its own isolated environment, keeping
things clean and organized.


πŸ“‚ Step 2: Create a Django Project

Once Django is installed, create a new project:

django-admin startproject project_name .
Enter fullscreen mode Exit fullscreen mode

Here, replace project_name with the name of your choice (e.g.,
myproject). The . at the end makes sure the project is created in
the current directory.


πŸ“¦ Step 3: Create a Django App

In Django, apps are modular components that handle specific
functionality (like blog, accounts, contact, etc.).

Run the following command to create an app:

python manage.py startapp app_name
Enter fullscreen mode Exit fullscreen mode

Replace app_name with your preferred name (e.g., contacts).


βš™οΈ Step 4: Configure Your Project

After creating the app, we need to tell Django about it. Open
settings.py inside your project folder and add your app to the
INSTALLED_APPS list:

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

πŸ‘€ Step 5: Database Setup & Superuser

Before we can use Django's built-in admin panel, let's set up the
database.

Run:

python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

This applies the default database migrations.

Now, create a superuser to log in to the admin panel:

python manage.py createsuperuser
Enter fullscreen mode Exit fullscreen mode

You'll be asked to enter: - Username - Email - Password


▢️ Step 6: Run Your Development Server

Start the server:

python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Visit your site in the browser:

Login using the superuser credentials you created.


πŸ“ Step 7: Create Your First Model

Models define the structure of your database tables. Let's create a
simple Contact model.

Open your app's models.py and add:

from django.db import models

class Contact(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
    phone = models.CharField(max_length=15)
    address = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name
Enter fullscreen mode Exit fullscreen mode

πŸ”„ Step 8: Apply Model Migrations

Whenever you add or modify models, you need to run migrations:

python manage.py makemigrations
python manage.py migrate
Enter fullscreen mode Exit fullscreen mode

This creates the necessary database tables.


πŸŽ‰ You're All Set!

Now you have a working Django project with: - A project setup\

  • An app added\
  • A database configured\
  • A model created

You can expand this project by creating views, templates, and URLs to
display your data.


βœ… Next Steps for You - Learn about Django views and
templates\

  • Build CRUD (Create, Read, Update, Delete) functionality\
  • Explore Django REST Framework for APIs

πŸ‘‰ This tutorial should help any beginner start their Django journey
smoothly.

Top comments (0)