Ajax 重定向后 Django 视图未呈现 [英] Django view not rendering after Ajax redirect

查看:26
本文介绍了Ajax 重定向后 Django 视图未呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我网站的主页顶部有多个按钮.每当按下这些按钮中的一个时,就会向 django 视图发送一个 get 请求,该视图被重定向并过滤 django 模型的查询集并最终显示在网页上.我知道我的 ajax 可以工作,因为终端说请求已正确重定向.它重定向到的函数似乎也在工作,因为它非常简单并且没有抛出任何错误.但是,我的观点保持不变,我不知道为什么.

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('')

推荐答案

您不能在您的情况下使用 Django 重定向.当您发送 ajax 请求时,您通常会期待一个 json 响应,并且基于此您可以通过您的 JavaScript 代码重定向用户.

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
});

<小时>

以下是您可以通过设置处理重定向的方法,您从 django 传回一个 JsonResponse 以及您想要转到的下一个页面:


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 })

在 JS 中,您使用完成(或成功)函数从传回的 JsonResponse 中获取 URL 并使用 window.location.href 重定向到该 URL:

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;
    }    
});

这篇关于Ajax 重定向后 Django 视图未呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆