Django Models & Database Tutorial

Create a Django Project and App with User Profiles

Codes With Pankaj
3 min readFeb 6, 2024

Step 1: Create a Django Project

1.1 Open your terminal or command prompt.

1.2 Navigate to the desired directory where you want to create your project.

1.3 Run the following command to create a new Django project named p4n_profiles

django-admin startproject p4n_profiles

1.4 Change into the project directory :

cd p4n_profiles

Step 2: Create a Django App

2.1 Inside the p4n_profiles project directory, run the following command to create a new Django app named members:

python startapp members

Step 3: Add App to INSTALLED_APPS

3.1 Open the file inside the p4n_profiles project directory.

3.2 Locate the INSTALLED_APPS list and add 'members' to it:


Step 4: Define a Model

4.1 Open the file inside the members app.

4.2 Import necessary modules:

from django.db import models

4.3 Define a simple UserProfile model with fields such as first_name, last_name, and email:

class UserProfile(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
email = models.EmailField(unique=True)

def __str__(self):
return f'{self.first_name} {self.last_name}'

Step 5: Make Migrations

5.1 Open your terminal in the project directory (p4n_profiles).

5.2 Run the following command to create migrations based on your model changes:

python makemigrations

5.3 Apply the migrations:

python migrate

Step 6: Run Initial Server

6.1 Start the development server:

python runserver

6.2 Open your web browser and go to to check if the project is running.

Step 7: Create Views

7.1 Open the file in the members app.

7.2 Use the UserProfile model in your views to interact with the database. For example, you can query and display user profiles:

from django.shortcuts import render
from .models import UserProfile

def display_profiles(request):
profiles = UserProfile.objects.all()
return render(request, 'members/profile_list.html', {'profiles': profiles})

Step 8: Create Templates

8.1 Create a new folder named templates inside the members app.

8.2 Inside the templates folder, create a new HTML file named profile_list.html:

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href=""
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>User Profiles</title>

<div class="container mt-4">
<h1>User Profiles</h1>

<table class="table">
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Email</th>
{% for profile in profiles %}
<td>{{ profile.first_name }}</td>
<td>{{ profile.last_name }}</td>
<td>{{ }}</td>
{% endfor %}

<!-- Bootstrap JS, Popper.js, and jQuery (required for Bootstrap) -->
<script src=""
<script src=""
<script src=""


Step 9: Create Superuser

9.1 Run the following command to create a superuser:

python createsuperuser

9.2 Follow the prompts to enter a username, email, and password for the superuser.

9.3 Use the superuser credentials to log in to the Django admin panel at

Step 10: Update URLs and file

10.1 Create a file inside the members app.

10.2 Define a URL pattern to link to the display_profiles view:

from django.urls import path
from .views import display_profiles

urlpatterns = [
path('profiles/', display_profiles, name='profile_list'),

10.3 Configure project URLs by updating the file in the p4n_profiles project:

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

urlpatterns = [
path('members/', include('members.urls')),

10.4 add code into

from django.contrib import admin
from .models import UserProfile

Step 11: Run Server and View Profiles

11.1 Start the development server:

python runserver

11.2 Open your web browser and go to to view the user profiles.

By following these steps, you should have successfully created a Django project, defined a model, migrated the database, displayed user profiles, and created a superuser for accessing the admin panel.
Download codes