Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to insert some text in all django context using django middleware

this my middleware code :

from django.conf import settings
from django.template import RequestContext

class BeforeFilter(object):
    def process_request(self, request):
        settings.my_var = 'Hello World'
        request.ss = 'ssssssssss'
        return None
    def process_response(self, request, response):

        return response

this is the settings.py:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.request',
)
MIDDLEWARE_CLASSES = (
    ...
    'middleware.BeforeFilter',
)

and the view is :

#coding:utf-8

from django.conf import settings
from django.shortcuts import render_to_response

from django.http import HttpResponse 
from django.template import RequestContext


def index(request):
    context = RequestContext(request)
    context['a'] = 'aaaa'
    return render_to_response('a.html',context)

the html is :

{{a}}fffff{{ss}}

but it not show {{ss}}:

aaaafffff 

so how do i show :

aaaafffffssssssss

how to insert some text in all django context using django middleware,

so that i cant use to insert the text everytime ,

thanks

like image 538
zjm1126 Avatar asked May 12 '11 04:05

zjm1126


People also ask

What is Get_response in Django middleware?

get_response(request) # Code to be executed for each request/response after # the view is called. return response. The get_response callable provided by Django might be the actual view (if this is the last listed middleware) or it might be the next middleware in the chain.

What can be achieved using middleware in Django?

In Django, middleware is a lightweight plugin that processes during request and response execution. Middleware is used to perform a function in the application. The functions can be a security, session, csrf protection, authentication etc.

Is context a keyword in Django?

A context is a variable name -> variable value mapping that is passed to a template. Context processors let you specify a number of variables that get set in each context automatically – without you having to specify the variables in each render() call.


2 Answers

To meet your initial goal, I do not think the BeforeFilter middle ware is required. What we need is just a template context processor.

Write a context processor as following:

#file: context_processors.py

def sample_context_processor(request):
   return {'ss':'ssssssssss'} #or whatever you want to set to variable ss

then add the context processor to TEMPLATE_CONTEXT_PROCESSORS list

#file: settings.py 

TEMPLATE_CONTEXT_PROCESSORS = (
    'myproject.context_processors.sample_context_processor',
)
like image 164
xiao 啸 Avatar answered Oct 07 '22 14:10

xiao 啸


You need to specify that you accessing request in the template. If you do just {{ss}} the variable does not exist since it an attribute of request (you did request.ss = 'ssssssssss', right?). So do {{request.ss}} in your template and it should work.

like image 37
Torsten Engelbrecht Avatar answered Oct 07 '22 15:10

Torsten Engelbrecht