Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
468 views
in Technique[技术] by (71.8m points)

python - Django view not rendering after Ajax redirect

The main page of my website has multiple buttons at the top. Whenever one of these buttons is pushed, a get request is sent to a django view, which is redirected and a queryset of django models is filtered and eventually displayed on the web page. I know that my ajax works because the terminal says the request is redirected properly. The function it redirects to also seems to be working, as it is quite simple and has not thrown any errors. However, my view remains the same and I'm not sure why.

urls.py

url(r'ajax_filter/', views.ajax_filter, name='ajax_filter'),
url(r'filter=(w+)/$', views.filtered_index, name='filtered_index'),

views.py

def filtered_index(request, filter):
    clothes = Clothes_Item.objects.filter(gender=filter)
    if request.user.is_authenticated():
        favorite_clothes_ids = get_favorite_clothes_ids(request)
        return render(request, 'test.html', {'clothes': clothes, 'favorite_clothes_ids': favorite_clothes_ids})
    else:
        return render(request, 'test.html', {'clothes': clothes, })

def ajax_filter(request):
    if request.is_ajax():
        gender_filter = request.GET.get('gender_filter') #filter type
        if gender_filter is not None:
            return HttpResponseRedirect(reverse('filtered_index', args=[gender_filter]))
    return HttpResponse('')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can not use Django redirect in your case. When you send an ajax request you usually expect a json response, and based on that you can redirect the user via your JavaScript code.

$.ajax({
  // You send your request here
}).done(function(data) {
  // You can handle your redirection here
});

Here is how you can handle a redirect with your setup, you pass back a JsonResponse from django with the next page that you want to go to:

from django.http import JsonResponse

def ajax_filter(request):
    if request.is_ajax():
        gender_filter = request.GET.get('gender_filter') #filter type
        if gender_filter is not None:
            return JsonResponse({
                'success': True,
                'url': reverse('filtered_index', args=[gender_filter]),
            })
    return JsonResponse({ 'success': False })

In JS, you use done (or success) function to grab the URL from the passed back JsonResponse and redirect to that URL using window.location.href:

$.ajax({
  // You send your request here
}).done(function (data) {
    if (data.success) {
        window.location.href = data.url;
    }    
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...