0

I try to test view calling by POST. I use follow=True. But test client uses GET method and my POST data are not passed.

here is my view:

def aaa(request):
    n = request.method
    d = request.POST
    template = 'shop/test.html'
    return render(request, template, d)

Here is my test:

from django.utils import unittest
from django.test.client import Client

def test_add_to_cart_page(self):
    response = self.client.post('/aaa/', {'product': 11}, follow=True)
    self.assertEqual(response.status_code, 200)

When the view is called. It is not POST, but GET used and my POST params are empty of course. Can somebody say why its happened?

EDIT: I made a clean venv with fresh Django. And it works as expected(calls POST) Looks like there is something rotten in the state of Denmark.

Joe Bobson
  • 1,216
  • 15
  • 19
  • 3
    Looks like you're using it correctly. Just to be sure... are you *assuming* that it's a GET request because you're not seeing d render in your template? Or did you actually inspect request.method? – dylrei Feb 04 '15 at 00:04
  • yes I inspect request.method – Joe Bobson Feb 04 '15 at 10:55

2 Answers2

2
follow=True

means that the client follows the redirection.

response = self.client.post('/aaa/', {'product': 11}, follow=True)

means that the response contains the followed response content. There is nothing wrong with your test; it must be doing a POST.


What's weird is that your view doesn't redirect to anything so I don't understand why you use follow=True. Also I don't see why you assume that post isn't working. What's the result of your test?

François Constant
  • 5,531
  • 1
  • 33
  • 39
  • It is simplified example – Joe Bobson Feb 04 '15 at 10:56
  • 1
    @JoeBobson It's tough to know how to help if we can't see the code that you're using. There are a couple obvious things wrong with the example code provided but then you say that you've verified the problem even though you're not showing anything that makes it look like you have. At this point, it's clear enough that you've made a mistake but since you aren't sharing the actual code, it's tough to help you find your error. – dylrei Feb 04 '15 at 15:26
0

After investigation I realize that using PREPEND_WWW breaks the test client post requests.

Joe Bobson
  • 1,216
  • 15
  • 19