achabacha322
achabacha322

Reputation: 651

How to send POST request with search term obtained from autocomplete in Django

I am using an auto complete form (Django-ajax-selects) which returns a dropdown list of items from the database. After a user clicks on an item, the search field is populated with the name of the item. I want to be able to take whatever term is in the search bar, and then after the user presses enter, the page navigates to the post the user entered into the search bar.

<form method="POST" action="/builds/show/">{% csrf_token %}

The url above in action needs to have the name of the item in the database appended to it. So if the name of the item in the database is First Post, the url in action should be:

<form method="POST" action="/builds/show/First__Build">{% csrf_token %}

What I want to know if there is any way to take the term from the input field, which looks like this in html:

<input type="text" class="form-control" placeholder="Search..." name="search" id="id_postField" onkeyup="searchOpen()" data-plugin-options="{&quot;min_length&quot;: 1, &quot;html&quot;: true, &quot;source&quot;: &quot;/builds/autocomplete/ajax_lookup/PostsTable&quot;}" data-ajax-select="autocomplete" autocomplete="off" value="" name="postField"></input>

I know how to do it in Django using named urls, but is there any way to get this done on the html side?

This is an example of how the ajax auto-complete looks like: Example

Upvotes: 0

Views: 570

Answers (1)

Medhat Gayed
Medhat Gayed

Reputation: 2813

You can use jquery to set the action attribute on the form like this:

$("#id_of_form").attr("action", "/builds/show/" + $("#id_postField").val())

Where #id_of_form is your form id or any other selector you may use to select your form.

Upvotes: 1

Related Questions