Friday, January 3, 2014

Re: Why 'break' in BaseHandler.get_response() self._view_middleware loop?

That follows the stated behavior for "process_view" middleware: https://docs.djangoproject.com/en/1.6/topics/http/middleware/#process_view

process_view is called before the request is passed on to the view function/class. The view is expected to return a response, so the only reason that the middleware should return a response is to circumvent the normal process and return an immediate response without ever calling the view. It's possible you want to use the "process_response" middleware instead if what you're really wanting to do is modify a response object. https://docs.djangoproject.com/en/1.6/topics/http/middleware/#process_response

_Nik

On 1/3/2014 3:53 PM, jeremiahsavage@gmail.com wrote:
Hello,

I was wondering why there is a break in this code?

                for middleware_method in self._view_middleware:
                    response = middleware_method(request, callback, callback_args, callback_kwargs)
                    if response:
                        break

The reason I ask is I was not able to use https://github.com/omarish/django-cprofile-middleware unless I commented out the last two lines.

My objects look like the following:

        self=<django.core.handlers.wsgi.WSGIHandler object at 0x7ff3c1320a90>

        self._view_middleware=[<bound method DebugToolbarMiddleware.process_view of <debug_toolbar.middleware.DebugToolbarMiddleware object at 0x7ff3ab62d750>>, <bound method ProfilerMiddleware.process_view of <django_cprofile_middleware.middleware.ProfilerMiddleware object at 0x7ff3ab62dad0>>, <bound method CsrfViewMiddleware.process_view of <django.middleware.csrf.CsrfViewMiddleware object at 0x7ff3ab62db90>>]



With the 'break' included as-is, the 'for' loop was exiting after the first iteration, as there is a 'response'.

        response=<django.template.response.TemplateResponse object at 0x7f8d978e1d50>

and the web page displays the following error:
------------------------------

AttributeError at /data/taxonomy/1/

'ProfilerMiddleware' object has no attribute 'profiler'
Request Method: GET
Request URL: http://152.16.15.249:8091/data/taxonomy/1/?prof
Django Version: 1.6.1
Exception Type: AttributeError
Exception Value:
'ProfilerMiddleware' object has no attribute 'profiler'
Exception Location: /home/jeremiah/.virtualenvs/auto_p3_d16/lib/python3.3/site-packages/django_cprofile_middleware/middleware.py in process_response, line 49

------------------------------------


Without the last two lines, I am able to use the profiler without errors.


Is the 'break' needed?

Thanks,

Jeremiah
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f3b6731c-d134-4af4-a7f3-092d92350fcd%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment