KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to write some unit tests for some Django json_view views and I'm having trouble passing the json_string to the view. I posted a related question yesterday about passing a json string to a Django view from the JS, the issue was that in my JS I was just passing the json string where I needed to be passing the string as the attribute of an object, because I was failing to do this the string was being taken as the key for the resulting query dict. I'm having a similar problem again except that this time it is form a Django unit test to the Django View. Here is a simplified version of my code which produces the same result. class MyTestCase(TestCase): def setUp(self): self.u = User.objects.create_user('test','test','test') self.u.is_active = True self.u.save() self.client.login(username='test',password='test') def test_create_object_from_form(self): """Test the creation of the Instance from the form data.""" import json json_string json.dumps({'resource':{'type':'book','author':'John Doe'}}) print(json_string) response = self.client.post(reverse('ajax_view'), {'form':json_string},'json') self.assetNotContains(response,'error') and the view looks like this @json_view def ajax_view(request): """Process the incoming form data.""" if request.method == 'POST': print(request.POST) form_data = json.loads(request.POST['form']) resource_data = form_data['resource'] form = MyUserForm(resource_data) if form.is_valid(): ... Here is what the two print statements produce when the test is run. The json_string is {"resource": {"type": "book", "author": "John Doe"}} and the query dict looks like <QueryDict: {u'{\'form\': \'{"resource": {"type": "book", "author": "John Doe"}}\'
Tags (comma-separated)
Save Edits
Cancel