亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Django 上使用 Javascript 正確獲取查詢

在 Django 上使用 Javascript 正確獲取查詢

www說 2023-04-01 17:28:15
我正在開發一個網站,我的首要任務之一是具有自動完成功能等的動態搜索表。我發現這可能使用ajax和 javascript。每當用戶在搜索字段中鍵入內容時,我已經實施了一個觸發器。當前的問題是從數據庫中獲取數據,現在我收到 404 錯誤,并且沒有返回任何數據。代碼:views.pydef search_bib(request):    if request.method == "POST":        search_str=json.loads(request.body).get('searchText')        bib = Bibrest51.objects.all().filter(            autor__starts_with=search_str) | Bibrest51.objects.all().filter(            autor__icontains=search_str)        data = Bibrest51.objects.all()                return jsonResponse(list(data), safe=False)記者:const searchField = document.querySelector("#searchField");searchField.addEventListener("keyup", (e) => {  const searchValue = e.target.value;  if (searchValue.trim().length > 0) {    console.log("searchValue", searchValue);    fetch("bibliografia-search", {      body: JSON.stringify({ searchText: searchValue }),      method: "POST",    })      .then((res) => console.log(res))      .then((data) => {        console.log("data", data);      })      .catch((e) => {        console.log(e);      });  }});任何幫助將不勝感激,我是該網站的新手,我希望這篇文章沒有做錯任何事。對于改進我的帖子的反饋也很感激 - 謝謝!
查看完整描述

1 回答

?
拉丁的傳說

TA貢獻1789條經驗 獲得超8個贊

首先,我注意到您的函數返回所有對象的數據。它應該返回 bib ,這是過濾后的結果。

其次,這是您的路線問題,因為該功能應該按預期工作


查看完整回答
反對 回復 2023-04-01
  • 1 回答
  • 0 關注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號