目前,我正在使用 TestCase 发出一个 HTTP Post 请求,该请求被重定向并且没有发布数据。我想在不重定向的情况下发出发布请求并保留数据。我该怎么做?
1)重定向的证据:
baby/urls.py
来自 django.conf.urls 导入模式、包含、url
urlpatterns = patterns('babies',
# Endpoints
url(r'^create_baby/$', 'views.create_baby'),
...
)
婴儿/测试.py
from django.test import TestCase
from django.test.client import Client
import pdb
class OrderCreationTest(TestCase):
def setUp(self):
self.normal_baby_data = {
'birth_time': {
'date' : '04-07-2013'
}
}
def test_baby_creation(self):
response = self.client.post('/consumer/create_baby/',
self.normal_baby_data,
follow=True
)
pdb.set_trace()
重击结果:
(Pdb) response.redirect_chain
[('https://testserver/consumer/create_baby/', 302)]
如果 follow=False 则永远不会创建视图的 create_baby 方法,也没有重定向链。bash 结果是:
(Pdb) response.redirect_chain
*** AttributeError: 'HttpResponseRedirect' object has no attribute 'redirect_chain'
2) 没有发布数据的证据
婴儿/views.py
def create_baby(request):
pdb.set_trace()
...
(Pdb) p request
<WSGIRequest
path:/consumer/create_baby/,
GET:<QueryDict: {}>,
POST:<QueryDict: {}>,
COOKIES:{'sessionid': 'f88ffoplh6lyytarn9n85z6bd5lefbzo'},
META:{u'CONTENT_TYPE': 'text/html; charset=utf-8',
u'CSRF_COOKIE': u'OC5LFOj8HvqTu0cKoAmty7vkYP0jNk3Q',
u'HTTP_COOKIE': u' sessionid=f88ffoplh6lyytarn9n85z6bd5lefbzo; Domain=None; expires=None; Max-Age=None; Path=/; secure',
u'PATH_INFO': u'/consumer/create_order/',
u'QUERY_STRING': '',
u'REMOTE_ADDR': '127.0.0.1',
u'REQUEST_METHOD': 'GET',
u'SCRIPT_NAME': u'',
u'SERVER_NAME': 'testserver',
u'SERVER_PORT': '80',
u'SERVER_PROTOCOL': 'HTTP/1.1',
u'wsgi.errors': <_io.BytesIO object at 0x10a6fa230>,
u'wsgi.input': <django.test.client.FakePayload object at 0x10b067b50>,
u'wsgi.multiprocess': True,
u'wsgi.multithread': False,
u'wsgi.run_once': False,
u'wsgi.url_scheme': 'https',