title: Sorting items by drag and drop in django
body: <p>In my django project I show a list of books in template. <strong>Book</strong> model has <strong>position</strong> field which I use to sort books.</p>
  
  <p>I'm trying to sort this list by drag and drop list items but my next code dont work well. I use <strong>JQuery UI</strong>. It works in frontend but dont change position field`s value when user drag and drop list item. Can someone help me to improve my js and view code. I am comfused. I would be grateful for any help.</p>
  
  <p><strong>models.py:</strong></p>
  
  <pre><code>class Book(models.Model):
      title = models.CharField(max_length=200, help_text='Заголовок', blank=False)
      position = models.IntegerField(help_text='Поле для сортировки', default=0, blank=True)
  
      class Meta:
          ordering = ['position', 'pk']
  </code></pre>
  
  <p><strong>html:</strong></p>
  
  <pre><code>&lt;div id="books" class="list-group"&gt;
  {% for book in books %}
    &lt;div class="panel panel-default list-group-item ui-state-default"&gt;
      &lt;div class="panel-body"&gt;{{ book.title }}&lt;/div&gt;
    &lt;/div&gt;
  {% endfor %}
  &lt;/div&gt;
  </code></pre>
  
  <p><strong>urls.py:</strong></p>
  
  <pre><code>url(r'^book/(?P&lt;pk&gt;\d+)/sorting/$',
       BookSortingView.as_view(),
       name='book_sorting')
  </code></pre>
  
  <p><strong>JS:</strong></p>
  
  <pre><code>$("#books").sortable({
        update: function(event, ui) {
              var information = $('#books').sortable('serialize');
              $.ajax({
                    url: "???",
                    type: "post",
                    data: information
              });
        },
  }).disableSelection();
  </code></pre>
  
  <p><strong>views.py:</strong></p>
  
  <pre><code>class BookSortingView(View):
      @method_decorator(csrf_exempt)
      def dispatch(self, request, *args, **kwargs):
          return super(BookSortingView, self).dispatch(request, *args, **kwargs)
  
      def post(self, request, pk, *args, **kwargs):
          for index, pk in enumerate(request.POST.getlist('book[]')):
              book = get_object_or_404(Book, pk=pk)
              book.position = index
              book.save()
          return HttpResponse()
  </code></pre>