« Adding Google Analytics to Sphinx Docs | Django's Summer of Code students announced! »
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.
Posted at 2:16 p.m. on April 16, 2009
Comments: 3
Tags: ajax , client , django , http , testing
Mocking an External Web Service in Python
Automating tests in Django
Announcing Django Crawler and django-test-utils
Welcome to the home of Eric Holscher on the web. I talk about software development, mostly in the realm of Django. I am interested in the real time web, testing, mobile apps, and other things.
Why Read the Docs matters
1 week, 5 days ago (Comments: 7)
Read the Docs Update
9 months, 4 weeks ago (Comments: 2)
Using Reviewboard with Git
1 year ago (Comments: 0)
Read the Docs Updates
1 year ago (Comments: 1)
Handling Django Settings Files
1 year ago (Comments: 12)
Required Reading
1 year, 2 months ago (Comments: 0)
Using Haystack to index non-database content
1 year, 2 months ago (Comments: 4)
Correct commands to check out and update VCS repos
1 year, 2 months ago (Comments: 0)
Site upgrades
1 year, 2 months ago (Comments: 0)
Building a Django App Server with Chef: Part 4
1 year, 2 months ago (Comments: 1)
Setting up Django and mod_wsgi
Building a Django App Server with Chef: Part 1
Screencast: Django Command Extensions
Big list of Django tips (and some python tips too)
Handling Django Settings Files
Lessons Learned From The Dash: Easy Django Deployment
Large Problems in Django, Mostly Solved: Delayed Execution
Building a Django App Server with Chef: Part 2


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