Skip to content

How to Build A Django App with ChatGPT: A Step-by-Step Guide

How to Build A Django App with ChatGPT

Django is a high-level Python web framework. It encourages rapid development and clean, pragmatic design. It is also one of the most popular web frameworks for building web applications. ChatGPT makes tasks a lot easier using its conversational and engaging AI. This post will show you just how easy this all is. And we’ll be doing so using ChatGPT to make it even simpler. 

So if you want a quicker way of learning, quicker results, or generally want to boost your productivity, then this post meets your needs!

In this article, we will walk you through the process of building a Django app with ChatGPT. We start by outlining the steps involved. Then provide a step-by-step guide to building and launching the app. By the end of this article, you will have a solid understanding of how to build a Django app with ChatGPT. You’ll also be able to create your own web applications and I’ll share a screenshot of the prompt we used to get this code.

Prerequisites:

Before we begin, there are a few things you’ll need to have installed on your computer. These will help you follow along with this tutorial. So let’s make sure you have them all:

  1. Python: Django is built with Python, so you’ll need to have Python installed on your computer. If you haven’t already, download the latest version of Python from the official website.
  2. pip: pip is the package installer for Python. You’ll need it to install Django and other Python packages. You can check if you have pip installed by running the following command in your terminal:

If pip is not installed, you can download and install it now. Following the instructions on the official website. Though pip is normally installed with your Python download.

  1. Django: Finally, you’ll need to install Django itself. You can do this using pip by running the following command in your terminal: 

This will download and install the latest version of Django to your machine.

With these prerequisites in place, you’re ready to start building your Django app!

Steps to Build a Django App

Start with ChatGPT! This was my initial prompt on how to build a Django app with ChatGPT.

I was specific about the output being a consistent flow of codes. This was so that different variables or incompatible approaches won’t be used in broken segments of codes which could need debugging.

Next, I asked it to recap all the steps used to build the app as we’ve used in this post: 

Before we dive into the tiny details of building a Django app, let’s first outline the steps involved. Here are the basic steps involved in building a Django app from ChatGPT:

  1. Install Django
  2. Create a new Django project
  3. Create a new Django app
  4. Add app name to the INSTALLED_APPS list
  5. Create a new model for the app
  6. Run migrations to create the necessary database tables
  7. Create a new view 
  8. Create a new template
  9. Create a base.html file 
  10. Add a URL pattern for the new view 
  11. Include the new URL pattern in the project’s urls.py file
  12. Launching the development server

We now have a mind map of steps to follow, let’s dive into the details.You’ll be sure to find the snippets of the step by step guide from ChatGPT used to build this app as we progress.

Step 1: Install Django

The first step in building a Django app is to install Django. You can install Django using pip, a package manager for Python. You may have already done this step to satisfy the requirements. If yes, you can proceed to step 2 below. Open your terminal or command prompt and run the following command: 

pip install Django

Step 2: Create a new Django project

Once you have installed Django, you can create a new Django project. A Django project is a collection of settings, configurations, and apps for a specific website. 

To create a new Django project, run the following command in the command line interface (CLI) or terminal of your computer. Navigate to the directory where you want to create the Django project (using cd your-choice-directory-name). 

Once you are in the desired directory, you can run the command to create the new Django project. 

django-admin startproject todo_project

From ChatGPT:

This will create a new directory called todo_project. It will contain all the necessary files for your project. That is the basic structure of a Django project, and you can see the folder created on my windows machine below. 

Step 3: Create a new Django app

A Django app is a self-contained module that provides specific functionality for your website. To create a new Django app, navigate to the root directory (myproject) of your project and run the following command: 

cd todo_project

python manage.py startapp todo

This command will create a new directory with the same name as ‘todo’. So be sure to customize that name if you’d like. This new directory will be in our project’s root directory. The new app directory will contain a set of files and directories that define the app’s functionality. This includes a views.py file, a models.py file, and a templates directory. By creating a new app, you can add new functionality to your Django project and organize your code in a modular way. 

Step 4: Add app name to the INSTALLED_APPS list

To add the todo app we’re building to your Django project, open the todo_project/settings.py file. Then include ‘todo’ in the INSTALLED_APPS list by simply typing it in.

This step is necessary to register the todo app as part of your Django project and make it available for use. By this, Django knows to include the todo app when it initializes the project. It allows you to use the models, views, and templates defined in the todo app within your project.

Step 5: Create a new model for the app

In this step, we are creating a new model in Django’s models.py file. Navigate to  todo/models.py and enter the code below. Save the models.py file after this edit.

from django.db import models

class TodoItem(models.Model):

    title = models.CharField(max_length=100)

    completed = models.BooleanField(default=False)

    def __str__(self):

        return self.title

This defines the structure and attributes of the to-do item. It enables us to easily manage and manipulate data related to our to-do items. 

The model is like the recipe for baking a cake. It tells you what ingredients you need and how to combine them to make the finished product. So, we use models to define what information is to be stored and how it should be organized. 

Step 6: Run migrations to create the necessary database tables

Migrations are a way to propagate changes you make to your models into your database schema. For instance, adding a field, deleting a model, etc.

Running migrations creates the necessary database tables based on your model definitions. It’s an important step to ensure that your Django app can work with your database properly. 

For our to-do app, running migrations will create the necessary tables to store our to-do items in the database. 

Simply enter the codes as shown below in your Command Line Interface (CLI) or terminal.

python manage.py makemigrations
python manage.py migrate

Step 7: Create a new view 

Great work so far! We have a few more easy steps to take. And in this one, we will be creating a new view in the todo/views.py file. 

Navigate to todo/views.py using you code editor and run the following code:

from django.shortcuts import render

from .models import TodoItem

def todo_list(request):

    items = TodoItem.objects.all()

    return render(request, 'todo/todo_list.html', {'items': items})

The view will handle a new functionality or feature in the Todo app. It defines what is displayed when a user navigates to a specific URL or endpoint in the app. Below is a sample code from my editor. Ensure yours before leaving the editor.

Step 8: Create a new template 

This is an important step, and we’ll do two things here.  First, create a directory under the todo directory (called templates). Then create another directory (called todo) under this new templates directory. Finally, we’ll create an HTML file for our app’s templates under this new todo directory. We’ll call this HTML file templates.html.

So we now have the path “todo/templates/todo/templates.html”. Enter the following code to build the display of our app’s page.

{% extends 'base.html' %}

{% block title %}

    To-do List

{% endblock %}

{% block content %}

 <h1>Congrats You've Successfully made Your Django App</h1>

 <h2>To-do List</h2>

 <ul>

 <li><input type="checkbox" checked> Buy groceries</li>

 <li><input type="checkbox" checked> Clean the house</li>

 <li><input type="checkbox"> Go for a run</li>

 <li><input type="checkbox"> Pay bills</li>

 <li><input type="checkbox"> Schedule doctor's appointment</li>

 <li><input type="checkbox"> Call mom</li>

 <li><input type="checkbox"> Finish work project</li>

 <li><input type="checkbox"> Organize closet</li>

 <li><input type="checkbox"> Plan vacation</li>

 <li><input type="checkbox" checked> Water plants</li>

 </ul>

 <h2>Little Motivation for You</h2>

 <ul>

 <li>Believe you can and you're halfway there. -Theodore Roosevelt</li>

 <li>You are never too old to set another goal or to dream a new dream. -C.S. Lewis</li>

 <li>Success is not final, failure is not fatal: it is the courage to continue that counts. -Winston Churchill</li>

 <li>Strive not to be a success, but rather to be of value. -Albert Einstein</li>

 <li>Believe in yourself! Have faith in your abilities! Without a humble but reasonable confidence in your own powers you cannot be successful or happy. -Norman Vincent Peale</li>

 </ul>

{% endblock %}

Why this hassle right? Well, one step in customizing the appearance of your Django To-Do app is to create a new template. This will allow you to define the HTML structure of your app’s pages. It also incorporates custom styling and functionality. Remember you can style this to show whatever you want to see on the page!

Prompt: 

ChatGPT response snippet:

Step 9: Create a base.html file 

We need a base HTML template to create a consistent look and feel across all pages of a Django web application. First, create a base.html file in the ‘todo/templates/’ directory.  Then fill it with the following code:

<!DOCTYPE html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title>{% block title %}{% endblock %}</title>

</head>

<body>

 <nav>

 <ul>

 <li><a href="{% url 'todo_list' %}">App Home</a></li>

 </ul>

 </nav>

 <main>

        {% block content %}

        {% endblock %}

 </main>

</body>

</html>

Your app is already looking good! Well done! 

The base template defines the basic structure and design elements that will be shared by all pages in our to-do app. This step is essential in order to maintain a uniform style throughout the app. 

Prompt:

ChatGPT response snippet: 

Step 10: Add a URL pattern for the new view 

To properly direct a user to a specific view in your Django app, you need to create a URL pattern. 

Navigate to todo/urls.py  and enter the following script:

You may need to create the urls.py Python file if it isn’t already there.

from django.urls import path

from . import views

urlpatterns = [

 path('', views.todo_list, name='todo_list'),

]

The lines of code map a specific URL to a corresponding view function. Now users can easily navigate through your app and access the appropriate content or functionality. 

Step 11: Include the new URL pattern in the project’s urls.py file

Now we configure the project’s URL dispatcher to recognize a new URL pattern. This will allow users to access a new functionality or page in our app. 

Navigate to the project’s urls.py file (ie. todo_project/urls.py) amd enter the code below:

from django.contrib import admin

from django.urls import include, path

urlpatterns = [

 path('admin/', admin.site.urls),

 path('', include('todo.urls')),

]

This is a crucial step in ensuring that users can navigate to the different parts of the application. It means users can access the features they need. 

Step 12: Launching the development server

The development server is a tool provided by Django to run the web application on your local machine. It allows developers to get a preview during the development phase. 

We’ll do this step in the CLI or terminal, and it’s just to run the code below: 

python manage.py runserver

By launching the development server, you can test and debug your Django app. You can evaluate its performance before deploying it to a live server. Here’s a sample of the expected output from your code:

Final Step: See you app in action!!!

Visit http://localhost:8000/ in your web browser to see the to-do list app in action!

Here’s a sneak peek of my Django to-do app with ChatGPT:

Conclusion

In this article, we have provided a step-by-step guide to building a Django app with ChatGPT. We started by outlining the basic steps involved and then provided a detailed guide to each step. By following these steps, you should now have a solid understanding of how to build a Django app with ChatGPT. So dive in and get your hands dirty, customize your Django app as much as you’d like! Or try building more with ChatGPT.

Resources 

Leave a Reply

Your email address will not be published. Required fields are marked *