title: How can I post data into test Client post request?
body: <p>I'm writing a test to see if form data is validating on post request trying to create a Post object.</p>
  <p><strong>tests.py</strong></p>
  <pre><code>def setUp(self):
      user = User.objects.create_user(email='test@gmail.com', password='test', name='test')
      self.user = User.objects.get(email='test@gmail.com')
      self.client.login(email=user.email, password=user.password)
  
  @tag('fast')
  def test_index(self):
      client = Client()
      response = client.post(reverse('index'), data={'user': self.user, 'body': 'Test'}, follow=True)
      print(response, self.user)
      self.assertTrue(Post.objects.filter(body='Test').exists())
  </code></pre>
  <p>but the test fails with message <code>False is not true</code> implying that the object with body <code>&quot;Test&quot;</code> was not created. I already tried encoding data with urlencode but it did not help.</p>
  <p>Here is what print statement shows: <code>&lt;TemplateResponse status_code=200, &quot;text/html; charset=utf-8&quot;&gt; test</code></p>
  <p><strong>views.py</strong></p>
  <pre><code>def index(request):
      posts = Post.objects.all()
      if request.method == 'POST':
          form = NewPostForm(request.POST, request.FILES)
          if form.is_valid():
              new_post = form.save(commit=False)
              new_post.user = user
              new_post.save()
              return redirect('index')
      else:
          form = NewPostForm(instance=None)
      context = {
          'form': form,
          'posts': posts
      }
  
      return render(request, 'index.html', context=context)
  </code></pre>
  <p>Maybe the user isn't being serialized correctly but when I remove it from data attribute (which would mean that the current user should be assigned to Post object) I get basically the same result.</p>