html
<div>
{{ productform.vendorid|as_crispy_field }}<a id="vendor_id_search" class="btn btn-info">search</a></br>
<div style="display:none;" id="show_vendorname">hjkh</div><br>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$("#vendor_id_search").click(function (event) {
event.preventDefault();
var vendor_id = $("#id_vendorid").val();
$.ajax({
url: '/ajax/find_vendorname/',
method: 'GET',
data: {
'vendor_id': vendor_id
},
dataType: 'json',
success: function (data) {
$("#show_vendorname").show(data)
}
});
});
視圖.py
#vendor_name_find_ajax
def find_vendorname(request):
if request.is_ajax():
vendorid = request.GET.get('vendor_id', None)
username = CustomUser.objects.filter(first_name=vendorid)
return username
我想在用戶鍵入 vendorid 欄位后顯示該 vendorid 的資料庫表中的相關名稱,一旦單擊該按鈕,相關名稱應顯示在該特定 div 上。在這里,我犯了一個錯誤,不知道要獲得那個值。
uj5u.com熱心網友回復:
$("#show_vendorname").show(data) 取消隱藏 div,但不要在資料中設定。
您需要先設定資料 $.html()
$("#show_vendorname").html(data).show()
編輯:
此外,您的視圖需要回傳一個HttpResponse:
from django.http import HttpResponse
def find_vendorname(request):
if request.is_ajax():
vendorid = request.GET.get('vendor_id', None)
username = CustomUser.objects.filter(first_name=vendorid)
return HttpResponse(username)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364052.html
上一篇:DjangoShell更新物件值
