Django 模板中的搜索字段
如何在类似于此图像的 Django 模板中创建搜索字段
http://asciicasts.com/system/photos/1204/original/E354I01.png
我在github上试试这个 https://github.com/rg3915/vendas/commit/e0c67fd8154a3b8e450ec3db38705cdd7efc1350
但我不知道如何完成。
最佳答案
您正在谈论搜索字段,但本质上只是一个表单,您有一个输入(搜索框)并且您收到该输入在您看来。
管理表单和 GET 操作的小例子:
views.py:
def your_view(request):
''' This could be your actual view or a new one '''
# Your code
if request.method == 'GET': # If the form is submitted
search_query = request.GET.get('search_box', None)
# Do whatever you need with the word the user looked for
# Your code
模板
在你的模板中,最重要的是表单,你应该有这样的东西:
# Your template code
<form type="get" action="." style="margin: 0">
<input id="search_box" type="text" name="search_box" placeholder="Search..." >
<button id="search_submit" type="submit" >Submit</button>
</form>
# Your template code
action='.'
action='/other/url/'
urls.py
您的 URL 文件必须与之前的相同。您不需要对您的实际 URL 做任何更改,它应该是这样的:
url(r'^your_url/?$', 'yourproject.views.your_view', name='your_url_name'),
无论如何,我建议您查看一些信息,例如:
https://stackoverflow.com/questions/27112729/
相关文章:
angularjs - Uncaught TypeError : X[g]. exec 不是函数 f
scala - 检查 Scala Set 中所有 'Seq' 元素的大小是否为 1 的最快方法是什么
python - 如何在 Python 3 中打印出编号列表
javascript - 非重复字符的最长子串javascript
haskell - Haskell 中带有 zipWith3 的 sum3
javascript - 在javascript中以毫秒为单位获取当前的unix时间