Testing AJAX Views in Django

A lot of the Django code we use at work has a special case for AJAX. It has been a kind of a pain to test, because the test client by default doesn't use AJAX. Luckily the is_ajax call in the Django HttpRequest object is a simple check of an HTTP Environmental variable.

An undocumented feature of the Django Test Client is that you can pass in custom HTTP ENV variables on requests. The definition of get for example is:

    def get(self, path, data={}, follow=False, **extra):

Later on in the file, the request environment is then updated with the extra keyword args: r.update(extra).

This lets us throw in arbitrary variables in our get and post requests in the test client. Like so:

  r = self.client.post('/ratings/vote/', {'value': '1',}, 
                                HTTP_X_REQUESTED_WITH='XMLHttpRequest')

Note that the custom env is outside of the dictionary of get parameters. This will now return the /ratings/vote/ view with the output that is normally called on an AJAX request.




Comments

1 Alex says...

Maybe I'm going crazy, but I'm fairly certain that this was actually documented at one point :/

Posted at 8:54 p.m. on April 16, 2009

2 布里斯班 says...

Finally some ajax approach to django! Thanks.

Posted at 4:49 a.m. on April 20, 2009

3 ben says...

Thanks for this, google first return and Just Works.

Posted at 3:18 p.m. on July 13, 2009

Comments support markdown

Comments are closed.

Comments have been close for this post.