从 html 页面上的输入框中提取值以插入到 views.py 函数中
Pulling Value from Input Box on html page to insert into views.py function
有类似的问题,但我似乎无法弄清楚。我在 html 页面上定义了一个输入框,如下所示:
<input id="charge_input" name="charge_field" placeholder="Charge Name" style="padding: 10px;" />
下面有一个按钮可以触发视图中的显示功能。
<button class="btn btn-info btn-lg" onclick="display()">SHOW DATA</button>
通过 ajax 调用:
function display()
{
var charge_field = $('#charge_field').text()
$.ajax(
{
url: 'display',
type: 'POST',
data: {"charge_field": charge_field},
success: function(output) {
$('#Scrape_Display').text(output);
}
}
)
}
我的最终目标是使用放置在上面输入框中的值,并使用该值在一对多 POSTGRES 数据库中进行过滤。这是函数:
@csrf_exempt
def display(request):
filtered_charges = request.POST.get('charge_field')
listed_inmate=Inmate.objects.filter(Charges__chargess__icontains=filtered_charges)
return render(request=request,
template_name='default.html',
context={'inmates': listed_inmates})
在这里,Inmate 是“一个”table,而 Charges 是“多个”table。 chargess 是 table 费用的列名称。我希望这很清楚,任何帮助都会有所帮助!谢谢
在你的函数中,你需要改变
var charge_field = $('#charge_input').text()
到
var charge_field = $('#charge_input').val()
编辑:您在获取值时使用 charge_field
作为 id 而不是 charge_input
。
有类似的问题,但我似乎无法弄清楚。我在 html 页面上定义了一个输入框,如下所示:
<input id="charge_input" name="charge_field" placeholder="Charge Name" style="padding: 10px;" />
下面有一个按钮可以触发视图中的显示功能。
<button class="btn btn-info btn-lg" onclick="display()">SHOW DATA</button>
通过 ajax 调用:
function display()
{
var charge_field = $('#charge_field').text()
$.ajax(
{
url: 'display',
type: 'POST',
data: {"charge_field": charge_field},
success: function(output) {
$('#Scrape_Display').text(output);
}
}
)
}
我的最终目标是使用放置在上面输入框中的值,并使用该值在一对多 POSTGRES 数据库中进行过滤。这是函数:
@csrf_exempt
def display(request):
filtered_charges = request.POST.get('charge_field')
listed_inmate=Inmate.objects.filter(Charges__chargess__icontains=filtered_charges)
return render(request=request,
template_name='default.html',
context={'inmates': listed_inmates})
在这里,Inmate 是“一个”table,而 Charges 是“多个”table。 chargess 是 table 费用的列名称。我希望这很清楚,任何帮助都会有所帮助!谢谢
在你的函数中,你需要改变
var charge_field = $('#charge_input').text()
到
var charge_field = $('#charge_input').val()
编辑:您在获取值时使用 charge_field
作为 id 而不是 charge_input
。