How to build a recipe app with Django

The recipe application allows us to manage our favorite recipes in a user-friendly and organized way. Whether we're seasoned chefs or just starting to explore the culinary world, this app provides a central location to store, access, and even share our recipes with others (depending on implementation choices). Using Django to create a recipe app requires a number of steps, including project setup, model, view, and template creation. Here is a step-by-step process on using Django to create a simple recipe app:

Step 1: Set up Django project

  • First, install Django using the following command:

pip install django
  • Create a Django project named myproject using the following command:

django-admin startproject myproject
  • Create a new Django app myapp for the recipe functionality. The Django app is a modular block of functionality within your Django project.

cd myapp
python manage.py startapp myapp

Step 2: Define models

Define Django models to represent category, recipes and related data, such as titles, author, ingredients, instructions and created_at in the myapp/models.py file.

from django.db import models
from django.contrib.auth.models import User
class Category(models.Model):
name = models.CharField(max_length=100)
class Recipe(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(User, on_delete=models.CASCADE)
ingredients = models.TextField()
instructions = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)

The above code represents two models for a recipe management system using Django:

  • Lines 4–5: Represents Category model which stores categories (e.g., "Breakfast", "Desserts") for recipes with a name field (max 100 characters).

  • Lines 7–12: Represents Recipe model which stores recipe details including title, author, ingredients, instructions, and creation date.

Step 3: Create views

Create Django views to handle user requests and render appropriate templates in myapp/views.py file.

from django.shortcuts import render, redirect, get_object_or_404
from .models import Recipe
from .forms import RecipeForm
def recipe_list(request):
recipes = Recipe.objects.all()
return render(request, 'myapp/recipe_list.html', {'recipes': recipes})
def recipe_detail(request, pk):
recipe = get_object_or_404(Recipe, pk=pk)
return render(request, 'myapp/recipe_detail.html', {'recipe': recipe})
def recipe_create(request):
if request.method == 'POST':
form = RecipeForm(request.POST)
if form.is_valid():
recipe = form.save(commit=False)
recipe.author = request.user
recipe.save()
return redirect('recipe_list')
else:
form = RecipeForm()
return render(request, 'myapp/recipe_form.html', {'form': form})
def recipe_edit(request, pk):
recipe = get_object_or_404(Recipe, pk=pk)
if request.method == 'POST':
form = RecipeForm(request.POST, instance=recipe)
if form.is_valid():
recipe = form.save(commit=False)
recipe.author = request.user
recipe.save()
return redirect('recipe_list')
else:
form = RecipeForm(instance=recipe)
return render(request, 'myapp/recipe_form.html', {'form': form})
def recipe_delete(request, pk):
recipe = get_object_or_404(Recipe, pk=pk)
recipe.delete()
return redirect('recipe_list')

The above code represents five views for a Django recipe management app:

  • Lines 5–7: List all recipes (recipe_list).

  • Lines 9–11: Shows details of a specific recipe (recipe_detail).

  • Lines 13–23: Creates a new recipe (recipe_create).

  • Lines 25–36: Edits an existing recipe (recipe_edit).

  • Lines 38–41: Deletes a recipe (recipe_delete).

Step 4: Define URLs

Map URLs to views in the Django app in myapp/urls.py file.

from django.urls import path
from . import views
urlpatterns = [
path('', views.recipe_list, name='recipe_list'),
path('recipe/<int:pk>/', views.recipe_detail, name='recipe_detail'),
path('recipe/new/', views.recipe_create, name='recipe_create'),
path('recipe/<int:pk>/edit/', views.recipe_edit, name='recipe_edit'),
path('recipe/<int:pk>/delete/', views.recipe_delete, name='recipe_delete'),
]

The above code maps URLs to views in a Django recipe management app. The root URL (/) shows a recipe list, while other patterns handle details, creation, editing, and deletion of recipes using their primary keys in the URL paths.

Step 5: Create templates

Create HTML templates to render recipe list and detail views.

myapp/
templates/
myapp/
recipe_list.html
recipe_detail.html
recipe_form.html
Hierarchy of the templates

Step 6: Configure URLs

Configure project-level URLs to include the app's URLs in myproject/urls.py file.

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]

The above code combines Django admin access (admin/) with custom app URLs (myapp/urls.py). The custom app likely handles functionalities like recipe management, becoming the default route for the application.

Step 7: Run migrations

Run Django migrations to create database tables for the models.

python manage.py makemigrations
python manage.py migrate

Step 8: Run server

Run the Django development server and navigate to the link in the widget below to view the recipe app.

python manage.py runserver

Implementation of recipe app

Now, we'll implement the steps of the recipe app with Django. Press the "Run" button to see how this application works.

from django.db import models
from django.contrib.auth.models import User

class Category(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Recipe(models.Model):
    title = models.CharField(max_length=200)
    ingredients = models.TextField()
    instructions = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.title
Building recipe app using Django

Conclusion

We are now able to build the application with Django. With the knowledge gained here, we can customize the app further and explore additional features according to our specific needs and preferences.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved