简体   繁体   English

如何发送带有从Django中的自动完成功能获得的搜索字词的POST请求

[英]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. 我正在使用自动完成表单(Django-ajax-selects),该表单从数据库返回项目的下拉列表。 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. 我希望能够接受搜索栏中的任何术语,然后在用户按Enter键之后,页面导航到用户在搜索栏中输入的帖子。

<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. 在上面的网址action需要有附加到它的数据库中的项目的名称。 So if the name of the item in the database is First Post , the url in action should be: 因此,如果在数据库中的项目的名称是First Post ,在url action应该是:

<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: 我想知道是否有任何方法可以从输入字段中提取该术语,这在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? 我知道如何在Django中使用命名的url来做到这一点,但是有什么办法可以在html上做到这一点?

This is an example of how the ajax auto-complete looks like: 这是ajax自动完成的示例: 例

You can use jquery to set the action attribute on the form like this: 您可以使用jquery在表单上设置action属性,如下所示:

$("#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. 其中#id_of_form是您的表单ID或您可以用来选择表单的任何其他选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM