1👍
In the end I decided to just test it for valid response and leave the data validation part to the form validation test, here’s what it looks like now:
def test_index(self):
client = Client()
get_response = client.get('/home/', {}, True)
post_response = client.post(reverse('index'), data={'body': 'Test'}, follow=True)
self.assertEqual(get_response.status_code, 200)
self.assertEqual(post_response.status_code, 200)
If anyone has any suggestions I would love to see them though because my particaular qestion problem still exists.
2👍
new_post.user = user
should probably be new_post.user = request.user
Then you don’t even need to pass user in the POST and it’ll just auto grab it from the request
The 200 status isn’t very helpful in this situation because no matter what (create an object or fail to create one) it’ll redirect to index
So my guess would be the form is failing the validation.
There’s not enough info for me to give a direct Answer, but here’s some steps to debug:
- Place a
print('Validation:', form.errors.as_data())
below+tabbed-left of theredirect('index')
- This will let you know if the form is failing it’s validation
- Place a
print('POST:', request.POST)
below+tabbed-right of therequest.method == 'POST':
- This will let you see if the data is actually getting there in the first place
- Re-Run the Tests
- Validation Error?:
- maybe it’s missing a field.
- maybe user is a required field and thus can’t pass validation without it.
- Allow user to be blank and/or null + set User in the
.is_valid()
(like you’re doing)
- Allow user to be blank and/or null + set User in the
- Post Data Empty?: there’s a direction, why?
- Validation Error?:
I just ran into a similar issue using Django 3.2.4, my data wasn’t getting there
I was using:
- attempt 1:
client.post('post/url', data=formdatadict, content_type='application/json')
- attempt 2:
client.post('post/url', data=json.dumps(formdatadict), content_type='application/json')
- My Solution:
client.post('post/url', data=formdatadict)
Note: I’m usingself.client.post()
butc = Client()
+c.post()
would work
Sooo hopefully this helps someone with their debugging
(especially because tests take so long to run)
0👍
You could try adding Content-Type:application/json
in the header of you request
as in the error message above it is showing the the request you are sending is in the plain text