title: django rest api getting KeyError: 'image'
body: <p>trying to create a function based post api</p>
  <p>views.py:</p>
  <pre><code>@api_view(['POST'])
  @permission_classes([IsAuthenticated])
  def post_blog(request):
      user = request.user
      data = request.data
      if data['image'] and len(data['image']) == 0:
          blog = Blog.objects.create(
              title=data['title'],
              description=data['description'],
              user=user
          )
      else:
          blog = Blog.objects.create(
              title=data['title'],
              description=data['description'],
              image=data['image'],
              user=user
          )
  
      serializer = BlogSerializer(blog)
      return Response(serializer.data)
  </code></pre>
  <p>models.py:</p>
  <pre><code>class Blog(models.Model):
      blog_id = models.AutoField(primary_key=True, editable=False)
      title = models.CharField(max_length=100, null=False)
      description = models.TextField(null=False)
      image = models.ImageField(null=True, blank=True)
      user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
      created_at = models.DateTimeField(auto_now_add=True)
      updated_at = models.DateTimeField(auto_now=True)
  
      def __str__(self):
          return self.title
  </code></pre>
  <p>serializers.py:</p>
  <pre><code>class BlogSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = Blog
          fields = ['blog_id', 'title','description', 'image', 'user', 'created_at']
  </code></pre>
  <p>after performing post request to &quot;POST http://localhost:8000/api/blogs/post/&quot; I am getting this error:</p>
  <pre><code>System check identified no issues (0 silenced).
  June 15, 2021 - 01:54:10
  Django version 3.2.4, using settings 'codersavvy.settings'
  Starting development server at http://127.0.0.1:8000/
  Quit the server with CTRL-BREAK.
  Internal Server Error: /api/blogs/post/
  Traceback (most recent call last):
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\django\core\handlers\base.py&quot;, line 181, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\django\views\decorators\csrf.py&quot;, line 54, in wrapped_view
      return view_func(*args, **kwargs)
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\django\views\generic\base.py&quot;, line 70, in view
      return self.dispatch(request, *args, **kwargs)
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\rest_framework\views.py&quot;, line 509, in dispatch
      response = self.handle_exception(exc)
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\rest_framework\views.py&quot;, line 469, in handle_exception
      self.raise_uncaught_exception(exc)
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\rest_framework\views.py&quot;, line 480, in raise_uncaught_exception
      raise exc
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\rest_framework\views.py&quot;, line 506, in dispatch
      response = handler(request, *args, **kwargs)
    File &quot;D:\work environment\Django_Api\codersavvy\lib\site-packages\rest_framework\decorators.py&quot;, line 50, in handler
      return func(*args, **kwargs)
    File &quot;D:\work environment\Django_Api\codersavvy\blog\views.py&quot;, line 75, in post_blog
      image=data['image'],
  KeyError: 'image'
  [15/Jun/2021 01:54:15] &quot;POST /api/blogs/post/ HTTP/1.1&quot; 500 96879
  
  </code></pre>
  <p>/<em>It looks like your post is mostly code; please add some more details. no more details to add</em>/</p>