【问题标题】:Supplying remote JSON data for Typeahead / Bloodhound with Django / Python使用 Django / Python 为 Typeahead / Bloodhound 提供远程 JSON 数据
【发布时间】:2021-10-25 11:00:57
【问题描述】:

我正在尝试复制 this Typeahead remote example 的功能,但我不知道如何以 Typeahead / Bloodhound 想要的方式提供数据,也不知道 datumTokenizerqueryTokenizer 的用途。

在 Python / Django views.py 我有:

nouns = ['apple', 'banana', 'pear']
return JsonResponse({'results': nouns})

以如下方式到达网站:

{"results": ["apple", "banana", "pear"]}

然而for 'kings' the example returns:

[{"year": "1949","value":"All the Kings Men","tokens":["All","the","Kings","Men"]}]

需要我们以这种格式返回它吗?如果是这样,怎么做?我们如何对示例进行简单的复制?

【问题讨论】:

    标签: json django ajax typeahead.js bloodhound


    【解决方案1】:

    想通了:实际使用return HttpResponse(nouns),或return JsonResponse(nouns, safe=False)

    如果出于安全考虑,请将其作为字典发送:

    noun_dicts = [{'suggestion':x} for x in nouns]
    return JsonResponse({'results':noun_dicts})
    

    然后在JS中解析dict。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      • 2014-08-24
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多