Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I jQuery ajax live search for the models in Django?

I tried live search with jquery and ajax, and even posted a question regarding this here, but there seems to have some serious problem somewhere in my view or in the ajax script I wrote. It searches and loads the content correctly.

But if I backspace and there's no value in the search form, it still shows me a list of value that I entered the first time. I think there's really a big problem in my code.

models.py:

class Status(models.Model):
    status = models.TextField()
    image = models.ImageField(upload_to=get_upload_file_name, blank=True)
    pub_date = models.DateTimeField(default=datetime.now)
    creator = models.ForeignKey(User, related_name="creator_set")
    likes = models.ManyToManyField(User, through="Like")

snippet of the .html:

        <input type="text" id="search" name="search" />
        <ul id="search-results">
        </ul>

views.py:

def search_status(request):

    if request.method == "GET":
        search_text = request.GET['search_text']
        if search_text is not None and search_text != u"":
            search_text = request.GET['search_text']
    else:
        search_text = ''  # I even tried using 0

    statuss = Status.objects.filter(status__contains = search_text)

    return render(request, 'ajax_search.html', {'statuss':statuss})

I have already loaded the jquery.min.js script in my template.

like image 679
Robin Avatar asked Sep 11 '13 14:09

Robin


1 Answers

I had to indent the else statement and the return statement in the views.py. And also to put statuss in the second if statement. And then it worked as I expected! Please guide me if there's any improvements to make. Thank you!

views.py:

def search_status(request):

    if request.method == "GET":
        search_text = request.GET['search_text']
        if search_text is not None and search_text != u"":
            search_text = request.GET['search_text']
            statuss = Status.objects.filter(status__contains = search_text)
        else:
            statuss = []

        return render(request, 'ajax_search.html', {'statuss':statuss})

This was the ajax script:

$(function() {

    $('#search').keyup(function() {

        $.ajax({
            type: "GET",
            url: "/status/search_status/",
            data: {
                'search_text' : $('#search').val(),
                'csrfmiddlewaretoken' : $("input[name=csrfmiddlewaretoken]").val()
            },
            success: searchSuccess,
            dataType: 'html'
        });
    });
});

function searchSuccess(data, textStatus, jqXHR)
{
    $('#search-results').html(data)
}

Snippet of index.html:

    <input type="text" id="search" name="search" />
    <ul id="search-results">
    </ul>

The included html:

{% if statuss > 0 %}
    <ul class="statuss">
        {% for status in statuss %}
            <li>
                <p>{{status}}</p>
            </li>
        {% endfor %}    
    </ul>
{% else %}
    <p>No status found.</p>
{% endif %}
like image 105
Robin Avatar answered Sep 22 '22 12:09

Robin