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中的类视图