Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

help with process_template_response django middleware

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

like image 586
ginad Avatar asked Mar 17 '11 02:03

ginad


People also ask

How do I use Django middleware?

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 .

What is security middleware in Django?

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.


1 Answers

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
like image 115
Yuji 'Tomita' Tomita Avatar answered Sep 28 '22 02:09

Yuji 'Tomita' Tomita