[Fixed]-Django Test Client post() returns 302 despite error on view's post()

29👍

It’s not totally clear why you’re getting a redirect, but if you want to follow it you need to tell RequestClient to follow redirects – per the documentation:

If you set follow to True the client will follow any redirects and a
redirect_chain attribute will be set in the response object containing
tuples of the intermediate urls and status codes.

So your test code should look like:

response = client.post("/app/mymodel/create/", follow=True)

It’d be worth checking the request chain to see where exactly it was routed.

👤Hamish

Leave a comment