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