title: Django: Error 500 (INTERNAL SERVER ERROR) when doing ajax call
body: <p>Hi when I'm doing ajax call, I'm getting this in console:</p>
  
  <pre><code>POST http://127.0.0.1:8000/registration/check/username/ 500 (INTERNAL SERVER ERROR) 
  </code></pre>
  
  <p>Also when I click on this link, I'm getting this:</p>
  
  <pre><code>DoesNotExist at /registration/check/username/
  User matching query does not exist.
  Request Method: POST
  Request URL:    http://127.0.0.1:8000/registration/check/username/
  Django Version: 1.3
  Exception Type: DoesNotExist
  </code></pre>
  
  <p>JQuery:</p>
  
  <pre><code>function check_username() {
      $("#id_username").change(function() {
          var user = $("#id_username").val();
          var status = $("#id_username").nextAll(".status").first().empty();
          var checking = '&lt;img src="/site_media/images/loader.gif" align="absmiddle"&gt; Checking availability...';
          var success = '&lt;img src="/site_media/images/tick.gif" align="absmiddle"&gt;';
          var e_length = '&lt;p&gt;User name have to be longer&lt;/p&gt;';
          if (user.length &gt;= 3) {
              status.append(checking);
              $.ajax({
                  url: "/registration/check/username/",
                  type: "POST",
                  data: { username : $("#id_username").val() },
                  dataType: "text",
                  success: function(msg) {
                      if (msg == '1') {
                          status.append(success);
                      }
                      else {
                          status.append("This username is already in use");
                      }
                  }
              });
          }
          else if (user.length &lt;= 3 &amp;&amp; user.length != 0) {
              status.append(e_length);
          }
          else {
              status;
          }
      });
  }
  </code></pre>
  
  <p>Html:</p>
  
  <pre><code>{% block main-menu %} 
      &lt;div class="contentarea"&gt;
              &lt;form method="post" action=""&gt;{% csrf_token %}
                  &lt;ul id="reg-form"&gt;
                      &lt;li&gt;
                          &lt;label for="id_username"&gt;Username:&lt;/label&gt;
                          &lt;input id="id_username" type="text" name="username" maxlength="30" /&gt;
                          &lt;div class="status"&gt;&lt;/div&gt;
                      &lt;/li&gt;
  </code></pre>
  
  <p>Urls.py:</p>
  
  <pre><code>...
  (r'^registration/check/([\w|\W]+)/$', register_check),
  ...
  </code></pre>
  
  <p>Views.py:</p>
  
  <pre><code>@csrf_exempt
  def register_check(request, variable):
      if request.is_ajax():
          if variable == 'username':
              user = User.objects.get(username__exact = request.POST['username']);
              if user:
                  msg = "1"
              else:
                  msg = '0'
          return HttpResponse(msg)
      else:
          return HttpResponse("0")
  </code></pre>