Django AJAX仅请求获取最后一个元素(不是getlist问题) [英] Django AJAX Request Only Getting Last Element (not getlist issue)

查看:72
本文介绍了Django AJAX仅请求获取最后一个元素(不是getlist问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Django中创建标记系统.基本上,我是通过AJAX将标签列表(表单中的复选框)传递到Django视图,该视图将使用httpresponse中新选择的相关标签来更新标签列表.

I'm trying to make a tagging system in Django. Basically I'm passing through a list of tags (checkboxes in a form) through AJAX to a Django view which will update the list of tags with a new selection of relevant tags in an httpresponse.

问题在于,即使在获得getlist之后,Django似乎也只能独自接收列表的最后一个元素.实际上,如果我打印整个request.GET,则它在每个列表中仅显示一个元素.

The problem is that Django only seems to be receiving the last element of the list on its own even after getlist. In fact, if I print the entire request.GET, it shows just one element in each list.

javascript/jQuery代码在这里:

The javascript/jQuery code is here:

    $(document).on('change', '.form-check-input',function () {

    var all_tags = $("input:checkbox").map(function() { return this.id; }).get();
    var selected_tags = $("input:checkbox:checked").map(function() { return this.id; }).get();

    alert(all_tags);
    alert(selected_tags);

    $.ajax({
        url: "{% url 'AJAX_tagFilter' %}",
        data: { 'all_tags': all_tags, 'selected_tags': selected_tags },
        cache: false,
        type: 'GET',
        success: function (data) {
            alert(selected_tags);
            $('#test').html(data);
            console.log('success');
        }
    });

});

我做了几次警报,以便可以看到在每个阶段传递的内容都是正确的.我看到了我期望的所有标签.

And I did a couple of alerts so that I can see what is being passed is correct at each stage. I see all the tags I expect it to.

12,13,21,16,17,15,11,7,18
12,13

但是当它进入Django视图时:

But when it gets to the Django view:

def getTagFilterSidebar(request):

if 'selected_tags[]' in request.GET:
    all_tags = request.GET.getlist("all_tags[]")
    selected_tags = request.GET.getlist("selected_tags[]")

    debug_text4 = str(request.GET)

我没有看到标签列表.这是输出:

I don't see the list of tags. This is the output:

<QueryDict: {'_': ['1539460657253'], 'all_tags[]': ['18'], 'selected_tags[]': ['13']}>

关键是它在我的本地服务器上似乎运行良好.但是,我正在使用Zappa并将其上传到AWS.仅在AWS上无法正常工作.因此,我对所发生的事情感到有些困惑.非常感谢您的帮助!

The critical part of this is that it seems to run fine on my local server. However, I'm using Zappa and have uploaded it to AWS. It's only on AWS that it isn't working right. So I'm a little puzzled as to what's happening. I'd really appreciate some help, thanks!

推荐答案

丹尼尔·罗斯曼(Daniel Roseman)建议:

As suggested by Daniel Roseman:

我没有传递列表,而是将两个变量中的join函数使用了逗号分隔的字符串:

Instead of passing a list, I used the join function in the two variables into a comma delineated string:

var all_tags = $("input:checkbox").map(function() { return this.id; }).get();
var selected_tags = $("input:checkbox:checked").map(function() { return this.id; }).get();

从那里,我在Django中使用split函数来逆转该过程:

From there, I used the split function in Django to reverse the process:

all_tags = request.GET.getlist("all_tags")[0].split(",")

不是解决问题的最直接方法,而是快速简便.

Not the most direct way of solving the problem, but quick and easy.

这篇关于Django AJAX仅请求获取最后一个元素(不是getlist问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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