AttributeError: 'TemplateResponse' object has no attribute '_reason_phrase'

564 views
Skip to first unread message

Web Architect

unread,
Nov 22, 2017, 3:29:42 AM11/22/17
to Django users
Hi,

We recently migrated from Django 1.8 to Django 1.11.7. We have an ecommerece site running on Django. When we are trying to access a page, following exception is occuring:

Traceback (most recent call last):

  File "/usr/local/lib/python2.7/wsgiref/handlers.py", line 85, in run

    self.result = application(self.environ, self.start_response)

  File "/virenv/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__

    return self.application(environ, start_response)

  File "/virenv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 161, in __call__

    status = '%d %s' % (response.status_code, response.reason_phrase)

  File "/virenv/lib/python2.7/site-packages/django/http/response.py", line 69, in reason_phrase

    if self._reason_phrase is not None:

AttributeError: 'TemplateResponse' object has no attribute '_reason_phrase'


I am completely clueless why the above exception is occurring. I do not have any other data or logs for the above exception


Could anyone help me in providing a way to debug the above?


Thanks.

Jason

unread,
Nov 22, 2017, 8:06:09 AM11/22/17
to Django users
what is the code for the render call?

Tim Graham

unread,
Nov 22, 2017, 11:14:59 AM11/22/17
to Django users
I tried a Google search for the last line of the error message and came to https://code.djangoproject.com/ticket/25964. Conclusion: try clearing your cache.

Web Architect

unread,
Nov 24, 2017, 4:04:31 AM11/24/17
to Django users
Figured out why the issue was occurring. I had written a cache decorator to cache based on per user:

def cache_per_user_method(ttl=None, prefix=None, cache_post=False):

    '''

    Decorator which caches the view for each User

    * ttl - the cache lifetime, do not send this parameter means that the cache

      will last until the restart server or decide to remove it

    * prefix - Prefix to use to store the response in the cache. If not informed,

      it will be used 'view_cache _' + function.__ name__

    * cache_post - Determine whether to make requests cache POST

    * The caching for anonymous users is shared with everyone


    How to use it:

    @cache_per_user_method(ttl=3600, cache_post=False)

    def get(self, request):

        ...

    '''

    def decorator(view_method):

        def apply_cache(obj, request, *args, **kwargs):


            CACHE_KEY = cache_key(request, prefix)


            logger.debug("cache key %s",CACHE_KEY)


            # Verifica se pode fazer o cache do request

            if not cache_post and request.method == 'POST':

                can_cache = False

            else:

                can_cache = True


            if can_cache:

                response = core_cache.get(CACHE_KEY, None)

            else:

                response = None


            if not response:

                response = view_method(obj, request, *args, **kwargs)

                logger.debug("cache not found in decorator")

                if can_cache and hasattr(response, 'render'):

                    logger.debug("cache set in decorator")

                    core_cache.set(CACHE_KEY, response.render(), ttl)

            return response

        return apply_cache


The above code was causing the error to happen but couldn't figure out where the issue was in the above. 


Thanks,

Reply all
Reply to author
Forward
0 new messages