I'm trying to write a django middleware using process_template_response but I seems like it's not working, can anybody help me with this or maybe give an example on how to use the 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 template
<!-- nothing is showing -->
{% block title %}{{ title }}{% endblock %}
by the way I'm using Django 1.3 rc 1
Thanks
Activating middleware To activate a middleware component, add it to the MIDDLEWARE list in your Django settings. A Django installation doesn't require any middleware — MIDDLEWARE can be empty, if you'd like — but it's strongly suggested that you at least use CommonMiddleware .
The django. middleware. security. SecurityMiddleware provides several security enhancements to the request/response cycle. Each one can be independently enabled or disabled with a setting.
http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-template-response
Are you using the new SimpleTemplateResponse
response classes?
Your view must return SimpleTemplateResponse
or subclass for the new process_template_response
middleware hook to work.
Copied from the docs:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With