Help with django middleware process_template_response

I am trying to write django middleware using process_template_response, but it seems to me that it is not working, can someone help me with this, or maybe give an example of how to use this method.

below is my code:

class MiddleWare(object): def process_template_reponse(self, request, response): response.context_data = dict(title='title') response.template_name = 'pages/helloworld.html' return response 

in settings.py

 MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'proj.app.middleware.MiddleWare', # here my middleware ) 

in the template

 <!-- nothing is showing --> {% block title %}{{ title }}{% endblock %} 

By the way, I am using Django 1.3 rc 1

thanks

+6
django middleware
source share
4 answers

http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-template-response

Are you using the new SimpleTemplateResponse response classes?

Your view should return a SimpleTemplateResponse or subclass for the new middleware hook process_template_response .

Copied from documents:

 def my_view(request): # Create a response response = TemplateResponse(request, 'mytemplate.html', {}) # Register the callback response.add_post_render_callback(my_render_callback) # Return the response return response 
+6
source share

TL; DR : you must display your templates using TemplateResponse :

 from django.template.response import TemplateResponse def myView(request): context = {'foo':'foo_value'} return TemplateResponse(request,'foo.html', context) 

The problem arises when you render your templates using the old old render imported from django.shorcuts . As stated in the documentation for process_template_response() , this hook is called when the response is TemplateResponse or equivalent. So what you need to do is use TemplateResponse to render your templates, and your middleware will work.

You can also use the TemplateResponse alias as render , so you do not need to change all of your views. I personally do not recommend this.

 from django.template.response import TemplateResponse as render 
+5
source share

The following is a working example of simple middleware using the process_template_response method.

 class ExampleMiddleware(object): def process_template_response(self, request, response): response.context_data['title'] = 'We changed the title' return response 

This middleware changes the value of the header variable in the template context data. Install this middleware by adding it to MIDDLEWARE_CLASSES in your settings file. Then go to any page in the admin django application, you will see the name of the page change. We changed the name.

+3
source share

I solved the problem by creating my own template tag. I'm just wondering how to add a context variable using process_template_reponse in middleware.

+2
source share

All Articles