Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been trying to switch my codes from Django function based view to class based view, but I'm having trouble understanding how to handle AJAX in Django CBV.

For example, suppose that I have this live search feature in my Django blog project. (sorry, I tried to make it as simple as possible.)

urls.py

url(r'^search/$', search_page),

forms.py

class SearchForm(forms.Form):
    query = forms.CharField()

entire_search_page.html

<form id="search-form" method="get" action=".">
    {{form.as_p}}
    <input type="submit" value="search" />
</form>

<div id="search-results">
    {% include "just_the_result.html" %}
</div>

just_the_result.html

{% for post in post_list %}
    <li>{{ post.title }}</li>

views.py

def search_page(request):
    form = SearchForm()
    post_list = []

    if 'query' in request.GET:
        form = SearchForm({'query': query})
        post_list = Post.objects.filter(title__icontains=query)

    if request.GET.has_key('ajax'):
        variables = RequestContext(request, {
            'post_list': post_list,
        })
        return render_to_response('just_the_result.html', variables)
    else:
        variables = RequestContext(request, {
            'form': form,
            'post_list': post_list,
        })
        return render_to_response('entire_search_page.html', variables)

search.js

$(document).ready(function() {
    $("#search-form").submit(search_submit);
});

function search_submit() {
    var query = $("#id_query").val();

    $("#search-results").load(
        "/search/?ajax&query=" + encodeURIComponent(query)
    );

    return false;
}

So basically, I'm showing the entire search page when the request is normal, while only showing the "just_the_result.html" if the request is AJAX. This works fine when I run the server, but I want to change this view to Class Based View.

This is what I have so far:

views.py

class PostSearch(ListView):
    model = Post
    template_name = 'app_blog/search.html'
    context_object_name = 'post_list'

    def get_queryset(self):
        queryset = super(PostSearch, self).get_queryset()

        query = self.request.GET.get('query')
        if query:
            return queryset.filter(title__icontains=query)
        else:
            return queryset

I guess this works correctly when the request is normal. However, I have no idea what to do when the request is an AJAX request! In the function based view, I could just return different templates and variables depending on whether the request is AJAX or not, but I don't think this is necessary or even possible in CBV. Also, one more question. I've been reading about RESTful design lately. Is my code above considered as "RESTful"?? I have trouble understanding how REST API is used exactly.

share|improve this question
add comment

1 Answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.