django中的类视图_在Django中测试Dajaxice视图

django中的类视图

If you want to test Dajaxice views from the Django test client, this might be your first approach:

如果要从Django测试客户端测试Dajaxice视图,这可能是您的第一种方法:

 url url = = '/dajaxice/apps.front.add_vote/'
'/dajaxice/apps.front.add_vote/'
data data = = {{ 'vote''vote' : : 'yes''yes' , , 'primary_key''primary_key' : : '1''1' }
}
response response = = selfself .. clientclient .. postpost (( urlurl , , datadata == datadata )
)

This doesn’t work for several reasons.

由于某些原因,这不起作用。

First of all, we need to simulate an AJAX request. Therefore the HTTP_X_REQUESTED_WITH header needs to be set. We can do this by simply passing it as a kwarg to the client.post method.

首先,我们需要模拟一个AJAX请求。 因此,需要设置HTTP_X_REQUESTED_WITH标头。 我们可以通过简单地将其作为kwarg传递给client.post方法来实现。

Furthermore, the default data encoding is multipart/form-data, which is not what we want. Dajax uses application/x-www-form-urlencoded encoding. We can solve this by setting the content_type kwarg.

此外,默认的数据编码是multipart / form-data ,这不是我们想要的。 Dajax使用application / x-www-form-urlencoded编码。 我们可以通过设置content_type kwarg来解决此问题。

The third problem is that we can’t simply urlencode the data dictionary directly. The JSON formatted payload should be the value of a key called argv.

第三个问题是我们不能简单地直接对数据字典进行urlencode。 JSON格式的有效负载应为名为argv的键的值。

翻译自: https://www.pybloggers.com/2013/03/testing-dajaxice-views-in-django/

django中的类视图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值