Skip to main content

Create a CRUD Application with Django: Complete Guide

· 5 min read
Jesus Paz
Python Expert & Solo Founder Empowering Developers

Are you tired of feeling stuck while trying to learn web development? Do complicated frameworks make you cringe? You're not alone! If you've ever thought about how to build a web application from scratch, you've come to the right place. In this guide, we will explore the exciting journey of building a CRUD (Create, Read, Update, Delete) application using Django, one of the most popular web frameworks in Python.

By the end of this tutorial, you’ll not only understand the fundamental concepts of Django but also possess the skills to create your very own web application. Whether you're a beginner or looking to brush up on your skills, this step-by-step guide will be your roadmap to success! Let's jump in!

What is CRUD?

Before we dive into the code, let’s clarify what CRUD stands for:

  • Create: Add new data.
  • Read: Retrieve and view data.
  • Update: Modify existing data.
  • Delete: Remove data.

These four operations form the backbone of any web application. In our case, we'll use Django to implement these functionalities seamlessly.

Setting Up Your Django Environment

Step 1: Install Django

To start building a CRUD app, you first need to have Django installed. You can do this with pip:

pip install django

Step 2: Create a New Project

Once Django is installed, create a new project:

django-admin startproject mycrudapp

Step 3: Start the Development Server

Navigate into your project directory and run the server:

cd mycrudapp
python manage.py runserver

Now, visit http://127.0.0.1:8000/ in your web browser. You should see the Django welcome page.

Creating Your First Django App

Step 4: Create an App

To follow the MVC structure, let's make an app where we will handle our CRUD functionalities:

python manage.py startapp products

Step 5: Register the App

Add your new app to the INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
...,
'products',
]

Designing the Database

Step 6: Create a Model

Define a model for your product:

from django.db import models

class Product(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
price = models.DecimalField(max_digits=10, decimal_places=2)

Run the migrations to create the database:

python manage.py makemigrations
python manage.py migrate

Implementing CRUD Operations

Step 7: Create Views for CRUD

Define views to handle your CRUD operations:

from django.shortcuts import render, redirect
from .models import Product

# Read operation
def product_list(request):
products = Product.objects.all()
return render(request, 'products/product_list.html', {'products': products})

# Create operation
def add_product(request):
if request.method == 'POST':
name = request.POST['name']
description = request.POST['description']
price = request.POST['price']
product = Product(name=name, description=description, price=price)
product.save()
return redirect('product_list')
return render(request, 'products/add_product.html')

Step 8: Create URLs

Link your views in urls.py:

from django.urls import path
from . import views

urlpatterns = [
path('', views.product_list, name='product_list'),
path('add/', views.add_product, name='add_product'),
]

Step 9: Templates for User Interface

Now, create simple HTML templates to display forms and lists. Here’s how a basic add_product.html might look:

<form method="POST">
{% csrf_token %}
<input type="text" name="name" placeholder="Product Name" required />
<textarea name="description" placeholder="Description"></textarea>
<input type="number" step="0.01" name="price" placeholder="Price" required />
<button type="submit">Add Product</button>
</form>

Testing Your Application

Step 10: Run and Test

You can run the server again and test your new CRUD application!

python manage.py runserver

Visit http://127.0.0.1:8000/ to see your product list, and navigate to /add/ to add new products.

Conclusion

Congratulations! You've just built a functional CRUD application with Django. This step-by-step tutorial has provided you with the foundational knowledge to expand your application further. You can now explore adding more features like search functionality, user authentication, or even deployment! Happy coding!

Frequently Asked Questions

Q: What is Django?

A: Django is a high-level Python web framework that promotes rapid development and clean, pragmatic design. It's perfect for building web applications quickly.

Q: Do I need to know Python to use Django?

A: Yes, Django is based on Python, so having basic knowledge of Python will make it easier for you to learn and understand Django.

Q: Can I create a RESTful API with Django?

A: Absolutely! Django is versatile and can be used to create RESTful APIs. You can utilize Django REST Framework for this purpose.

Q: How do I deploy my Django app?

A: You can deploy your Django app using various platforms, such as Heroku, AWS, DigitalOcean, or even your own server. The process generally involves setting up a web server and configuring your project accordingly.

Q: Is it difficult to learn Django?

A: Learning Django can be challenging, but with the right resources and continuous practice, you’ll find that it’s a friendly framework for web development.

Conclusion

In this tutorial, we've unlocked the basics of creating a CRUD application with Django, a beloved framework for Python developers. Remember the key takeaways: setting up your environment, building models, creating views, and rendering templates are the foundations of your web application. Now, it’s time to put this knowledge into practice! Explore new features, enhance your application, and, most importantly, have fun building! If you enjoyed this tutorial, consider sharing it with others who might find it helpful!