VOOZH about

URL: https://www.geeksforgeeks.org/python/a-guide-to-sending-data-using-cache-in-django/

⇱ Caching in Django - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Caching in Django

Last Updated : 28 Jan, 2026

Django caching improves performance by storing frequently used data and reducing repeated database queries.

  • Stores commonly accessed data for faster retrieval.
  • Reduces database load and unnecessary queries.
  • Improves response time for users.
  • Helps build faster and more reliable Django applications.
👁 web_application
Caching

Sending Data Using Cache in Django

Consider a project named 'core' having an app named 'src'.

File Structure:

Install Required Packages

pip install django-redis

Redis is an in-memory data store used in Django as a high-performance caching backend to enable fast data access and reduce database load.

Note: Redis must be installed and running on the system before using caching features.

Register the app in settings.py

Configure Cache in settings.py

Define Models

In src/models.py:

The models define the database structure. Category stores recipe categories, and Recipe stores recipe details linked to a category.

Register Models in Admin

In src/admin.py:

This enables model management through the Django admin panel.

Create Views with Caching

In src/views.py:

The home view retrieves all recipes from the database. The view_recipe view checks the cache first and fetches data from the database only if it is not already cached.

Configure URLs

URLs defined at the project level route requests to views inside the src app.

Creating GUI

In src/templates/home.html: This HTML page is used to view the home page of the project.

In src/templates/view.html: This HTML file is used to present the data which is being saved by the cache.

Apply Migrations

python manage.py makemigrations
python manage.py migrate

Run the Server

python manage.py runserver

Output

👁 Image

👁 nexthtml
Comment