ColeGulledge
ColeGulledge

Reputation: 573

Pulling Value from Input Box on html page to insert into views.py function

there are similar questions to this, but I can't seem to figure this out. I have an input-box defined on my html page as this:

<input id="charge_input" name="charge_field" placeholder="Charge Name" style="padding: 10px;" />

With a button below to trigger the display function in views.

<button class="btn btn-info btn-lg" onclick="display()">SHOW DATA</button>

With an ajax call:

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);
                    
                }
            }
        )
    }

My end goal, would be to use the value placed into the above input box, and use that value to filter in a one-to-many POSTGRES database. This is the function:

@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})

Here, Inmate the "one" table, and Charges is the "many" table. chargess is the column name of the table Charges. I hope this is clear, and any help would help! Thanks

Upvotes: 0

Views: 66

Answers (1)

John Maged
John Maged

Reputation: 185

Here in your function, you need to change

var charge_field = $('#charge_input').text()

to

var charge_field = $('#charge_input').val()

EDIT: You're using charge_field as id instead of charge_input while grabbing the value.

Upvotes: 1

Related Questions