title: django and jQuery - inline delete and record update
body: <p>I am new to django and I have some difficulties. I have a table with multiple (100s of) records.</p>
  
  <p>I want to use jQuery to enable inline updating and deletion of a record. </p>
  
  <h3>HTML:</h3>
  
  <pre><code>&lt;p&gt;The current candidate list is:&lt;/p&gt;
      &lt;div id="display1"&gt;
          &lt;span&gt;
            &lt;button class="delete_button" id="1"&gt;del&lt;/button&gt;
          &lt;/span&gt;
          &lt;span style="width:50px;"&gt;
            &lt;button class="editdiv" id="1"&gt;EDIT&lt;/button&gt;
          &lt;/span&gt;
          &lt;span&gt;
              &lt;a href="/candidates/1/"&gt;jake wilmott&lt;/a&gt;
          &lt;/span&gt;
          &lt;span&gt;
              de vere 
          &lt;/span&gt;
          &lt;span&gt;
              Nepal 
          &lt;/span&gt;
          &lt;span&gt;
              hands off 
          &lt;/span&gt;
          &lt;span&gt;
              connected 
          &lt;/span&gt;
          &lt;span&gt;
              nice guy 
          &lt;/span&gt;
      &lt;/div&gt;
      &lt;div class="noshow" id="edit1"&gt;
          &lt;span style="width:100px;"&gt;&lt;button class="update" id="1"&gt;update&lt;/button&gt;&lt;/span&gt;
          &lt;span&gt;
              jake wilmott
          &lt;/span&gt;
          &lt;span style="width:220px;"&gt;
              &lt;input class="company" type="textfield" style="width:210px;" id="new_company1" value="de vere" /&gt;
          &lt;/span&gt;
          &lt;span&gt;
              &lt;input type="text" id="new_country1" value="Nepal" /&gt;
          &lt;/span&gt;
          &lt;span&gt;
              &lt;select id="new_status1"&gt;
                  &lt;option value="no contact"&gt;no contact&lt;/option&gt;
                  &lt;option value="hands off"&gt;hands off&lt;/option&gt;
                  &lt;option value="ongoing"&gt;ongoing&lt;/option&gt;
                  &lt;option value="sent email"&gt;sent email&lt;/option&gt;
                  &lt;option value="waiting"&gt;waiting&lt;/option&gt;
                  &lt;option value="trash"&gt;trash&lt;/option&gt;
              &lt;/select&gt;
          &lt;/span&gt;
          &lt;span&gt;
              &lt;input type="text" id="new_connection" value="connected" /&gt;
          &lt;/span&gt;
          &lt;span style="width:220px;"&gt;
              &lt;input type="textfield" style="width:210px;" id="new_notes" value="nice guy" /&gt;
          &lt;/span&gt;
      &lt;/div&gt;
  </code></pre>
  
  <h3>jQuery functions are:</h3>
  
  <pre><code>//delete
  &lt;script&gt;
      $(document).ready(function() {
          $(".delete_button").click(function() {
              alert('delete script')
              var id = $(this).attr('id');
              alert(id)
              $.ajax({
                  type: "POST",
                   url: "/candidates/",
                  data: { id:id },
                  success: function(response){
                      alert(response);
                  }
              });
              return false;
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p><code>alert('delete script')</code> alerts and so does <code>alert(id)</code>. But then it breaks down and nothing happens</p>
  
  <h3>Updated jQuery is:</h3>
  
  <pre><code>&lt;script&gt;
      $(document).ready(function() {
          $('.update').click(function() {
              alert('script now')
              var id = $(this).attr('id');
              var company = $("#new_company" + id).val()
              var country = $("#new_country" + id).val()
              var status =  $("#new_status" + id).val()
              alert(status)
              $.post('/candidates/' + id + company + country + status + '/', function() {
                  alert('to here')
                  //$this.replaceWith("&lt;span class='success'&gt;Liked&lt;/span&gt;");
                  jQuery(data["html"]).appendTo(".success");
              });
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>Again <code>alert('script now')</code> and <code>alert(status)</code> both dutifully alert and then it breaks down again.</p>
  
  <h3>urls.py is:</h3>
  
  <pre><code>from django.conf.urls import patterns, include, url
  
  urlpatterns = patterns('',
      # Examples:
      url(r'^candidates/$', 'candidates.views.index'),
      # url(r'^$', 'amore.views.index', name='index'),
      url(r'^candidates/(?P&lt;id&gt;\d+)/$', 'candidates.views.detail'),
      url(r'^candidates/(?P&lt;id&gt;\d+)/$', 'candidates.views.delete'),
      url(r'^update/(\d+)/$','candidates.views.update'),
  </code></pre>
  
  <h3>views.py is:</h3>
  
  <pre><code>from django.template import Context, loader
  from candidates.models import Candidates
  from django.http import HttpResponse
  from django.shortcuts import render_to_response, get_object_or_404
  
  def index(request):
      return HttpResponse("Hello, world. You're at the candidate index.")
  
  def index(request):
      data = Candidates.objects.all()[:5]
      t = loader.get_template('candidates/index.html')
      c = Context({
          'data': data,
      })
      return HttpResponse(t.render(c))
  
  def update(request, id):
      candidate = Candidates.objects.get(pk = id)
      candidate.company = request.POST.get('company')
      candidate.country = request.POST.get('country')
      candidate.status = request.POST.get('status')
      candidate.notes = request.POST.get('notes')
      candidate.save()
      return HttpResponse('updated')
  
  def delete(request, id):    
      candidate = Candidates.objects.get(pk = id)
      candidate.delete()
      return HttpResponse('this record has been deleted')
  
  def detail(request, id):
      p = get_object_or_404(Candidates, pk=id)
      return render_to_response('candidates/detail.html', {'Candidates': p})
  </code></pre>
  
  <p>I have googled hundreds of pages but I just cannot solve this. </p>
  
  <p>So:</p>
  
  <ol>
  <li>what am I doing wrong?</li>
  <li>what should the URL be in the jQuery script to pass the variables to <code>views.py</code></li>
  <li>Is the <code>urls.py</code> file correct?</li>
  </ol>
  
  <p>Please help me make it work!</p>