Thursday, September 29, 2011

Re: problem with middleware - correctly formatted

On Thu, Sep 29, 2011 at 2:18 AM, jenia ivlev <jenia.ivlev@gmail.com> wrote:
> Hello:
>
> This is the second version of my question. I reformatted it.
>
> I have defined a middleware class. and i have added it to the
> middleware_classes attribute in setting. When a request comes in, the
> middleware class gets created (the debugger catches the code when the
> breakpoint is on the class CommonFiilter(): line)
>
> Now i expect the function  def process_template_response(self,
> request, response): to get called. I have debug point on the inside of
> the function and the debugger never traps the execution. The debugger,
> though, traps the execution at the line where the function name and
> parameters are defined.
> This is the class:
>
> class CommonFilter():#<---- debugger breaks here
>    def process_template_response(self, request, response): #<---
> debugger breaks here
>        if response.template_name=='store/index2.html': #<--- NOT
> HERE(or after this line)
>            catnames=getCategories()
>
> response.context_data.update({'catnames':catnames,'user':request.GET.get(key='user',default=None)})
>        return response
>
> Also tried this:
>
> class CommonFilter():#<---- debugger breaks here
>    def process_template_response(self, request, response):#<----
> debugger breaks here
>        if response.template_name=='store/index2.html':#<--- NOT
> HERE(or after here)
>            catnames=getCategories()
>            response.context_data['catnames']=catnames
>
> response.context_data['user']=request.GET.get(key='user',default=None)
>        return response
>
> Just in case, this is the setting MIDDLEWWARE_CLASSES variable:
>
>    MIDDLEWARE_CLASSES = (
>                      'store.models.CommonFilter',
>    'django.middleware.csrf.CsrfViewMiddleware',
>    'django.middleware.common.CommonMiddleware',
>    'django.contrib.sessions.middleware.SessionMiddleware',
>    'django.middleware.csrf.CsrfViewMiddleware',
>    'django.contrib.auth.middleware.AuthenticationMiddleware',
>    'django.contrib.messages.middleware.MessageMiddleware',
>    )
>
> store is an app in this project and ofcourse CommonFilter is defined
> in models.py.
>
> Why is the function process_template_response function not being
> executed?
>
> Thanks for your time and kind concern.
>

Could you please confirm you are using Django 1.3, and show your view
function. I want to confirm that TemplateResponse is available in your
version of Django and that you are returning a SimpleTemplateResponse
or subclass.

One thing is that your middleware is at the top of the list. This
means that it will be run first during the request phase
(process_{request,view}), but last during the response phase
(process_{template_response,response,exception}).

However, in the process_template_response portion of the
response_phase, there is no way to 'short circuit' the processing of
middleware, so all should run.


Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment