How to share common data in all Django templates

Hello World, Welcome to ProjectsPlaza.com. Today I am going to discuss how to share common data in all Django templates. In this tutorial, we will show common data in all templates with the help of a context processor.
According to the official documentation:
context_processors is a list of dotted Python paths to callables that are used to populate the context when a template is rendered with a request. These callables take a request object as their argument and return a dictionary of items to be merged into the context.

We will create a very simple example. In this example, we will create some view methods and their relative template. Then we will pass dictionary data to context processor and show in all templates.
I have assumed that you have basic knowledge of Django. If you are new to Django then please see our beginner tutorials.
I have divided this tutorial into the following steps:
  • Create shareData app and add to project
  • Create two view method index and show with their relative HTML templates
  • Create a URL and add to the project to show the template view
  • Create a third view method to pass data in a context processor
  • Access the data in both templates


Create shareData app and add to project

Please run the following command to create a new app in your exiting project.

py manage.py startapp shareData

Open project level settings.py file add the newly created app in the INSTALLED_APPS list.


Create two view method index and show with their relative HTML templates

Open your app-level views.py file and add the following methods.

# index
def index(request):
    return render(request,'index.html')

# show
def show(request):
    return render(request,'show.html')

Create templates directory in the shareData folder and create index.html and show.html file with the following code.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Index Page</title>
</head>
<body>
    <h3>Index Page</h3>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Show Page</title>
</head>
<body>
    <h3>Show Page</h3>
</body>
</html>

Create a URL and add to the project to show the template view

Create urls.py file in shareData app and add the following code.

from django.urls import path
from . import views
urlpatterns=[
    path('',views.index,name='index'),
    path('show',views.show,name='show'),
]

Create a third view method to pass data in a context processor

Create a common method in views.py file and add the following code.

# Common
def common(request):
    return {
        'common':'I am everywhere'
    }

Now open project-level settings.py file and the following code in the context_processors list. Following is the example.

context_processor


Access the data in both templates

Now open index.html and show.html file add the following code after heading.

<p>{{common}}</p>

Now when you will access the template, you will see the same data in both templates. You can change the data and recheck the page.


I hope you are enjoying the tutorials. Please share the article and add your feedback in the comment section.

1 thought on “How to share common data in all Django templates”

  1. Pingback: How to share common data in all Django templates – ProjectsPlaza – Django Projects, Laravel Projects Codeigniter Projects

Leave a Reply