Django的AJAX

I'm trying to incorporate a simple AJAX function with Django and I've no idea what I'm doing wrong. request.is_ajax() is returning False and even when I do event.preventDefault(), the form is still being submitted.

Here is the view:

def search(request):
    if request.is_ajax():
        search_text = request.POST.get('search_text', False)
    else:
        search_text = 'Not AJAX'
    return HttpResponse(search_text)

The JS:

$(function(){

    $('#search_form').submit(function(event){
        event.preventDefault();

        $.ajax({
            type: 'post',
            url: {% url 'users:search' %},
            dataType: 'html',
            data: {
                'search_text': $('#search').val(),
                'csrfmiddlewaretoken': '{{ csrf_token }}'
            },
            success: function(data){
                $('#search-results').html(data);
                },
            failure: function(data){
                alert('Got an error');
                }
        });
        return false;
    });

});

And the HTML:

 <div class="col-md-4">
            <h5>Search</h5>
            <form action="{% url 'users:search' %}" method="POST" id="search_form" name="search_form">
                {% csrf_token %}
                <input type="text" id="search" name="search">
                <input type="submit" value="Search">
            </form>
            <ul id="search-results">
                {% if users %}
                    {% for user in users %}
                        <li><a href="{% url 'users:detail' user.id %}">{{ user.username }}</a></li>
                    {% endfor %}
                    {% else %}
                        <p>No user available</p>
                {% endif %}
            </ul>
        </div>

I believe the form is actually always submitted because you have an error in your js. In the $ajax options, the url should be surrounded with quotes:

$.ajax({
    type: 'post',
    url: '{% url 'users:search' %}',
    ...
});

In your question you say that request.is_ajax is False, which implies the ajax call is actually performed. I don't think that's the case here. Given the above code sample the call does not happen at all and none of the view code should be executed.

If you have separated the js from the html, why are you using the template language in the js.

url: '{% url 'users:search' %}',

I think you should just do:

url: 'search',

I've figured out what went wrong.

I changed my JavaScript code to:

$(document).ready(function(){

    $('#search_form').submit(function(event){

        event.preventDefault();

        $.ajax({
            type: 'POST',
            url: '/users/search/',
            data: {
                'search_text': $('#search').val(),
                'csrfmiddlewaretoken': $('input[name=csrfmiddlewaretoken]').val() // properly capture the CSRF Token
            },
            success: function(data){
                $('#search-results').html(data);
                },
            failure: function(data){
                alert('Got an error');
                }
        });

    });
});

The error was the CSRF Token wasn't being sent with the rest of the data. Hopefully someone can find this useful down the road. Silly mistake by my part.