Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Introduction – We learned earlier that the variables we want to use in the template are passed in from the parameters of the context in the view function. Each view function passes whatever parameters it needs. But suppose we have so many problems now, and I need to pass the same variable to multiple different templates, do I need to add context to each template’s corresponding view function and use it to pass? B: Sure, but won’t it be a bother? Here comes our great ** context handler! ** We use it to pass the specified variable to all templates at once, for each template to use!!

Context Processor

1. What is a context handler? What does it do?

  • Context processors are also called context renderers. But I prefer to call it processor. After all, the translation for processor is “processing.” But it makes sense to call it a context renderer, rendering is closer to using the scene.
  • Djangos Context Processor is a template that performs some of the rendering of a page, known as page rendering.
  • And context, in computer programming terms, is context.

In other words, the same processor can respond differently to different environments.

  • To borrow an example from the Internet: when a user visits a site, all pages of the site should be able to display the user’s own IP address.

For this function, we need to obtain the IP address of the visiting user from the request and render it on the page. You might imagine that we could do this in the view function for each page, but it would be too much trouble. The best way is to define the process once and then use it on every page. At this point, we can help by customizing a context handler. Render different IP addresses on the page through the same processor for users from different sources.

Context handlers create template variables, but they are different from passing parameters using context. Using context handlers can provide the same variables for each template (that is, pass custom variables to all templates at once so that each template can use them)

2. Take a look at the context configuration items in Django

In settings.py, contains the context handler currently in use. Its role is to provide the same variables for each template.

3. Custom context handler && usage

(1) Create a new file named “myContexProcessor.py” in the home directory for writing custom context handlers:

This is the implementation of the online example mentioned above, so add the code to get the user’s IP address to this file.

(Note: Custom context handlers can also be placed anywhere the project can be imported.)

Detailed steps:

  1. A file myContextProcessor.py is created in the home directory.
  2. Create a myTest function in the myContextProcessor.py file
  3. The request parameter must be passed
  4. An object of type dictionary must be returned.

(2) Register mytest with context Settings. Py:

Find the location of the file and add the custom context handler functions. (TEMPLATES – > OPTIONS – > context_processors)

(3) Add tags to the page template:

Once you’ve done that, a Django call to a template is processed by a context handler, and the resulting dictionary of data is passed to the template to retrieve data from template tags.

At this point, we can open the development server and access the test.

🔆 In The End!

Start now, stick to it, a little progress a day, in the near future, you will thank you for your efforts!

This blogger will continue to update the basic column of crawler and crawler combat column, carefully read this article friends, you can like the collection and comment on your feelings after reading. And can follow this blogger, read more crawler in the days ahead!

If there are mistakes or inappropriate words can be pointed out in the comment area, thank you! If reprint this article please contact me for my consent, and mark the source and the name of the blogger, thank you!