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:
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 myapppython manage.py startapp myapp
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 modelsfrom django.contrib.auth.models import Userclass 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.
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_404from .models import Recipefrom .forms import RecipeFormdef 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.userrecipe.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.userrecipe.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
).
Map URLs to views in the Django app in myapp/urls.py
file.
from django.urls import pathfrom . import viewsurlpatterns = [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.
Create HTML templates to render recipe list and detail views.
myapp/templates/myapp/recipe_list.htmlrecipe_detail.htmlrecipe_form.html
Configure project-level URLs to include the app's URLs in myproject/urls.py
file.
from django.contrib import adminfrom django.urls import path, includeurlpatterns = [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.
Run Django migrations to create database tables for the models.
python manage.py makemigrationspython manage.py migrate
Run the Django development server and navigate to the link in the widget below to view the recipe app.
python manage.py runserver
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
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