0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately, whenever I go to filter the table: "Charges" in views.py it is not recognizing the variable "filtered_charges" and is returning "None".
@ColeGulledge Check my edit, you should be using the ID instead of the name.
perfect! Thanks John! Great first contribution!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.