Table of Contents
- Introduction
- Prerequisites
- Setup
- Creating the Django Project
- Creating a Recipe App
- Managing Recipes
- Conclusion
Introduction
In this tutorial, we will create a Recipe App using the Django web framework. The Recipe App will allow users to view, add, update, and delete recipes. We will cover the basics of Django, including setting up the project, creating models and database tables, implementing views and templates, and handling user input.
By the end of this tutorial, you will have a working Recipe App that you can further customize and expand to fit your needs. This tutorial assumes you have a basic understanding of Python programming and web development concepts.
Prerequisites
Before starting this tutorial, you should have the following:
- Basic knowledge of Python programming language.
- Python installed on your system.
- Pip package manager installed.
- Basic knowledge of HTML and CSS.
Setup
Let’s begin by setting up our development environment:
- Open a terminal or command prompt.
- Create a new directory for your project and navigate to it.
- Run the following command to create a new virtual environment:
python3 -m venv myenv
- Activate the virtual environment:
- For Windows:
myenv\Scripts\activate
- For macOS and Linux:
source myenv/bin/activate
- Install Django:
pip install django
Now that our environment is set up, let’s move on to creating the Django project.
- Install Django:
Creating the Django Project
- In the terminal, navigate to your project directory if you’re not already there.
- Run the following command to create a new Django project named “recipeapp”:
django-admin startproject recipeapp
This will create a new directory named “recipeapp” with the necessary files and folders for a Django project.
- Change into the “recipeapp” directory:
cd recipeapp
- Run the following command to start the Django development server:
python manage.py runserver
You should see output similar to the following:
Starting development server at http://127.0.0.1:8000/ Quit the server with CONTROL-C.
Congratulations! You have successfully created a new Django project.
Creating a Recipe App
Now let’s create a Django app within our project to handle recipes:
- In the terminal, while inside the “recipeapp” directory, run the following command:
python manage.py startapp recipes
This will create a new directory named “recipes” with the necessary files and folders for a Django app.
- Add the “recipes” app to the project’s settings:
- Open the “settings.py” file located in the “recipeapp” directory.
- Find the
INSTALLED_APPS
list and add'recipes'
to the end of the list. - Save the file.
Now the “recipes” app is added to our project.
- Define the Recipe model:
- Open the “models.py” file in the “recipes” directory.
- Define the Recipe model class as follows:
from django.db import models class Recipe(models.Model): title = models.CharField(max_length=100) ingredients = models.TextField() instructions = models.TextField() def __str__(self): return self.title
This model represents a recipe with a title, ingredients, and instructions. The
__str__
method is used to display the title when the model object is printed.
- Apply the model changes to the database:
python manage.py makemigrations python manage.py migrate
These commands create the necessary database tables based on the defined model.
- Create a superuser:
- Run the following command:
python manage.py createsuperuser
- Enter a username, email (optional), and password for the superuser.
Now we have the basic structure in place. Let’s move on to managing the recipes in our app.
Managing Recipes
We will now create views and templates to handle the display and management of recipes.
- Create a views file:
- In the “recipes” directory, create a new file named “views.py”.
- Add the following code to the file:
from django.shortcuts import render, get_object_or_404 from .models import Recipe def recipe_list(request): recipes = Recipe.objects.all() return render(request, 'recipes/recipe_list.html', {'recipes': recipes}) def recipe_detail(request, pk): recipe = get_object_or_404(Recipe, pk=pk) return render(request, 'recipes/recipe_detail.html', {'recipe': recipe})
These views retrieve the list of recipes and an individual recipe object and pass them to the corresponding templates.
- Create templates:
- In the “recipes” directory, create a new directory named “templates”.
- Inside the “templates” directory, create another directory named “recipes”.
- Inside the “recipes” directory, create two HTML templates named “recipe_list.html” and “recipe_detail.html”.
- Add the following code to “recipe_list.html”:
{% for recipe in recipes %} <h2>{{ recipe.title }}</h2> <p>{{ recipe.ingredients }}</p> <p>{{ recipe.instructions }}</p> <hr> {% empty %} <p>No recipes available.</p> {% endfor %}
- Add the following code to “recipe_detail.html”:
<h2>{{ recipe.title }}</h2> <p>{{ recipe.ingredients }}</p> <p>{{ recipe.instructions }}</p>
These templates define the structure and content for displaying the list of recipes and an individual recipe.
- Define URLs and routing:
- Open the “urls.py” file located in the “recipeapp” directory.
- Add the following code:
from django.urls import path from recipes import views urlpatterns = [ path('', views.recipe_list, name='recipe_list'), path('recipe/<int:pk>/', views.recipe_detail, name='recipe_detail'), ]
These URLs map to the corresponding views we defined earlier.
- Test the app:
- Start the Django development server if it’s not already running:
python manage.py runserver
- Open your web browser and go to http://localhost:8000/.
- You should see the list of recipes.
Congratulations! You have successfully created a Recipe App using Django.
Conclusion
In this tutorial, we have built a Recipe App using Django. We covered the basics of creating a Django project, setting up an app, defining models, creating views and templates, and routing URLs. You can further enhance the app by adding features like user authentication, a search function, or the ability to add/edit/delete recipes.
Django is a powerful framework for web development, and understanding its fundamentals will enable you to build a wide range of web applications. Keep exploring Django’s documentation and try implementing additional features to enhance your Recipe App and gain further experience with Django development.
Remember to practice, experiment, and have fun building Django projects!