'HttpResponse' object has no attribute 'url' when using Client() for tests

1,684 views
Skip to first unread message

Marcus

unread,
Oct 15, 2014, 9:20:52 AM10/15/14
to django...@googlegroups.com
Hi,

I just updated Django from an older version to 1.6.7 and some of my tests are now failing while trying to follow redirects.

In [1]: from django.test import Client

In [2]: import django

In [3]: django.VERSION
Out[3]: (1, 6, 7, 'final', 0)

In [4]: c = Client()

In [5]: r = c.get('/click/1/', follow=True)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-507677d1861e> in <module>()
----> 1 r = c.get('/click/1/', follow=True)

/usr/local/lib/python2.7/site-packages/django/test/client.pyc in get(self, path, data, follow, **extra)
    473         response = super(Client, self).get(path, data=data, **extra)
    474         if follow:
--> 475             response = self._handle_redirects(response, **extra)
    476         return response
    477

/usr/local/lib/python2.7/site-packages/django/test/client.pyc in _handle_redirects(self, response, **extra)
    603         response.redirect_chain = []
    604         while response.status_code in (301, 302, 303, 307):
--> 605             url = response.url
    606             redirect_chain = response.redirect_chain
    607             redirect_chain.append((url, response.status_code))

AttributeError: 'HttpResponse' object has no attribute 'url'

In [6]: r = c.get('/click/1/')

In [7]: r.status_code
Out[7]: 302

As far as I can see I am doing it like the documentation says (https://docs.djangoproject.com/en/1.6/topics/testing/tools/#making-requests). I if downgrade to Django 1.5 follow=True works as it should.


Tom Evans

unread,
Oct 15, 2014, 11:31:40 AM10/15/14
to django...@googlegroups.com
I think you are returning a redirect that is not a sub class of
HttpResponseRedirectBase - ie this will not work:

rsp = HttpResponse(content='Content Found', status=301)
rsp['Location'] = 'http://foo/bar'
return rsp

as it is not a HttpResponseRedirectBase.

This was introduced by e94f405, which fixes case #18558

https://code.djangoproject.com/ticket/18558

I think this fix is wrong, since it means that one kind of
HttpResponse is treated differently to the equivalent, derived type.

Cheers

Tom

Marcus

unread,
Oct 16, 2014, 4:48:10 PM10/16/14
to django...@googlegroups.com

Thanks for your answer and your assumption is correct, that's how the  redirect looks.

A quickfix in the test was to use a response without follow=True and response['location'] to test the redirect (which only went one step).

Marcus
Reply all
Reply to author
Forward
0 new messages