title: How to make a POST request using DJANGO REST framework with ajax
body: <p>This is my model class:</p>
  
  <pre><code>    class PoliceAssurance(models.Model):
          Numpolice = models.IntegerField()
          Raison = models.CharField(max_length=50)
          Tel = models.IntegerField()
          Email = models.CharField(max_length=50)
  </code></pre>
  
  <p>and here is my serializer:</p>
  
  <pre><code>    class PoliceSerializer(serializers.ModelSerializer):        
          class Meta:
              model = PoliceAssurance
              fields = ('Numpolice','Raison','Tel','Email');
  </code></pre>
  
  <p>Now, I need to make a POST request through an AJAX call. Could anyone please provide information on how I may approach this task?</p>
  
  <p>this is my views.py</p>
  
  <pre><code>@login_required(login_url="login/")
  def home(request):
      return render(request,"home.html")
  
  class PoliceViewset(generics.ListCreateAPIView):    
      queryset = PoliceAssurance.objects.all()
      serializer_class =  PoliceSerializer    
  </code></pre>
  
  <p>and my urls.py</p>
  
  <pre><code>    urlpatterns=[
      url(r'^$', views.home, name='home'),
      url(r'PoliceAssurance',views.PoliceViewset.as_view(),              name='PoliceAssurance'),
      ]
  </code></pre>
  
  <p>this my ajax request </p>
  
  <pre><code>$(#suit).click(function(){          
  
      var data = {};
      data.Numpolice = $(#num).val();
      data.Raison = $(#raison).val();
      data.Tel = $(#tel).val();
      data.Email = $(#email).val();
  
      $.ajax({
          type: "POST",
          url: "/PoliceAssurance/",
          data: data,
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function(data){
              alert(data);
          },
          failure: function(errMsg) {
              alert(errMsg);
          }
      });
  });
  </code></pre>