django_ context manager

Repeat code views may be implemented in the context manager

Logic implementation is to first go over the request url specified is the views of the function function, and then we go to the specified context manager logic, finally rendering the page as follows:

We project to create a new user directory .py file (process_content.py) and then write the logic implemented in this file, to achieve complete logic need to import it in settings-templates-options

 

to sum up:

Usage scenarios: template inheritance reduce duplication define the content of the page, the page content reuse. Personal navigation bar on the right side of the blog are inherited base page so that the greatest degree of code reuse. But when the parent templates dynamic data, these dynamic data in the sub-template is not displayed. We can be solved by custom context processor.

Structure: Context Manager filename usually context_processors.py, def xx (request) to a custom function parameters necessary request, which will be returned as the contents of the dictionary to all templates, templates {{key}} Value .

Configuration: it needs to be configured in settings where 

Note: In django manager up and down, try not to write too heavyweight code, because the code in the context of the renderer, each request will be executed once, if this renderer is too heavy, it will very much affect the performance

Guess you like

Origin www.cnblogs.com/mhmh007/p/12118603.html