title: What can cause a 500 error in a HttpResponse to an AJAX call?
body: <p><strong>EDIT</strong> mayor changes were made to the question and text to make it clear. As it turns out the error is caused by a very stupid mistake which is that I forgot to import HttpResponse to my views.py. Since in the same views.py I handle another view, I assumed it was imported. Rookie mistake. :(</p>
  
  <p><strong>Question:</strong>
  I'm attempting to submit up or down votes via ajax. Apparently most of the code in my view is executed, then when returning a response it fails. The problem is that there are no errors displayed by django; the only info I get in the terminal is this: "POST /c/vote/ HTTP/1.1" 500 10814. Where /c/vote/ is the URL that should handle votes. </p>
  
  <p>The console in Chrome doesn't help much either. The error I get is "Failed to load resource: the server responded with a status of 500 (INTERNAL SERVER ERROR)", then if I click the link I'm redirected to /c/vote/ where django returns a 404. <strong>Note</strong> I'm voting in a different url (i.e. <code>/c/&lt;country&gt;/</code>)</p>
  
  <p>I found out that votes are actually being saved, or deleted so the problem may be when returning a response.</p>
  
  <p>My template (custom template tags to know if the user voted before): </p>
  
  <pre><code>&lt;div class="vote_buttons" x:id="{{linkpost.pk}}"&gt;
      &lt;a href="#" class="upVote{% if linkpost|is_up_voted_by:user %} voted{%endif%}" x:value="1" &gt;&lt;/a&gt;
      &lt;a href="#" class="downVote{% if linkpost|is_down_voted_by:user %} voted{%endif%}" x:value="-1"&gt;&lt;/a&gt;
  </code></pre>
  
  <p> </p>
  
  <p>The js code (I'm using JQuery). The variable {{ vote_url }} is passed with a template tag.:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function() {
          $(".vote_buttons").bind("vote", function(event, value) {
              var vote_el = $(this);
              $.ajax({
                      type:'POST',
                  url: '{{ vote_url }}',
                  data: {
  
                      'pk': vote_el.attr("x:id"),
                      'delta': value
                  },
                  dataType: "json",
          success : function(data, textStatus, jqXHR) {
                      switch (data.voted_as) {
                          case 1:
                              vote_el.find("a.upVote").addClass("voted");
                              vote_el.find("a.downVote").removeClass("voted");
                         break;
                          case -1:
                              vote_el.find("a.upVote").removeClass("voted");
                              vote_el.find("a.downVote").addClass("voted");
                         break;
                      }
                  },
              });
          });
          $('.upVote, .downVote').click(function(){
              $(this).parent().trigger("vote", $(this).attr("x:value"));
              return false;
          });
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>And finally the view. I added some print statements to figure out where does it fails. Thanks to @Steven the code executes all the way to Step 12. Then the previously mentioned error occurs. </p>
  
  <pre><code>def vote(request):
      """
      Likes or dislikes a linkpost.
      """
      print "Setp 1 ok!"
      #User must be authenticated to vote.
      if request.is_ajax():
          print "Step 2 ok"
          if request.method == 'POST' and request.user.is_authenticated():
              print "Step 3 ok"
              delta = request.POST['delta']
              # In case an error occurrs with delta value
              try:
                  delta = int(delta)
                  print "Step 4 ok"
              except ValueError:
                  print "Error was value error"
                  return HttpResponse("{'success': 'false'}")
  
              # You can only vote upwards or downwards    
              if not delta in (1, -1):
                  print "Error was in delta"
                  return HttpResponse("{'success': 'false'}")
              print "Step 5 ok"
              #We check if the linkpost actually exists!
              LinkPost = get_model('company', 'LinkPost')
              try:
                  linkpost = LinkPost.objects.get(pk=request.POST['pk'])
                  print "Step 6 ok"
              except LinkPost.DoesNotExist:
                  print "Link object does not exist"
                  return HttpResponse("{'success': 'false'}")
  
              #We check if the user voted before.
              Vote = get_model('company', 'Vote')
              try:
                  vote = Vote.objects.get(linkpost = linkpost, listener = request.user)
                  print "Step 7 ok!" 
              except Vote.DoesNotExist:
                  print "Vote doesn't exists!" 
                  vote = None
  
              # If there is already a vote
              if vote:
  
                  print "Step 8."
                  if vote.delta == delta:
                      vote.delete()
                  else:
                      print "Step 9."
                      vote.delta = delta
                      vote.save()
  
              #There wasn't a vote, we create one.
              else:
                  print "Step 10."
                  Vote.objects.create(linkpost = linkpost,
                                             listener = request.user,
                                             delta = request.POST['delta'])
  
              response_dict = {'success' : 'true', 'voted_as': delta}          
              print "Step 12."    
              return HttpResponse(simplejson.dumps(response_dict), mimetype="application/json")
          else:
              print "User not authenticated"
              raise Http404('What are you doing here?')
      else:
          print "Request isn't ajax"
          raise Http404('What are you doing here?')
  </code></pre>
  
  <p>Any help figuring out the problem will be highly appreciated! If the question is still unclear I'll try to clarify or add as much info as possible!</p>