简体   繁体   English

Django数据库查询搜索

[英]Django database query search

I am using Django (2.1, python 3.6) and I would like to alter my project files to search a database and have the results returned to and displayed in an HTML file. 我正在使用Django(2.1,python 3.6),我想更改项目文件以搜索数据库,并将结果返回并显示在HTML文件中。 The search is simple, it should look at one field (that contains a single word) in a model. 搜索很简单,它应该查看模型中的一个字段(包含一个单词)。 Based on its simplicity, this is not a complex lookup and I consider Q objects to be overkill. 基于其简单性,这不是一个复杂的查询,我认为Q对象是过大的。

Thus far, I have yet to get a search that returns results. 到目前为止,我还没有获得返回结果的搜索。 My general issue is that I am not entirely grasping the process and when I look online, either the documentation shows its examples in a shell, I find examples where there is some lacking information that the reader is assumed to know (eg the model, url, imported module, file name for inserted code), the example is out of date, or there is still something that I am overlooking. 我的一般问题是,我并没有完全掌握流程,当我在线查看时,要么文档在外壳中显示了其示例,要么在示例中找到了一些读者应该知道的缺少信息的示例(例如,模型,URL ,导入的模块,所插入代码的文件名),示例已过时,或者仍然有我忽略的内容。

My understanding of the general process is below, but depending on how I do the view, I think the urls.py should be in there somewhere: 下面是我对一般过程的理解,但是根据我的看法,我认为urls.py应该在某个地方:

form (base.html) -> view.py -> query (unknown file) -> results in template (search_results.html)

There is certainly data in the database, but when I place a query term into the form and press submit , absolutely nothing happens. 数据库中肯定有数据,但是当我将查询词放入表单中并按submit ,绝对没有任何反应。 Here is a smattering of my code/files. 这是我的一些代码/文件。

samples/models.py 样本/ models.py

class Sample(models.Model):
   sample_name = models.CharField('Sample', max_length=16)

samples/templates/base_generic.html 样本/模板/ base_generic.html

<form name="sample_search_form" method="GET" action="{% url 'search' %}">
     <input  id="sample_search_box" type="text" name="sample_search_box"  placeholder="Search samples..." >
      <button id="sample_search_submit" type="submit" >Submit</button>
</form> 

NOTE I tried using sample_search_list instead of search for the url, but this caused an error upon submit, and I am fairly certain this word should match the definition in the views.py file. 注意我尝试使用sample_search_list而不是search url,但这在提交时导致错误,并且我相当确定此词应与views.py文件中的定义匹配。

samples/views.py 样本/ views.py

def search(request):
    if request.GET:
        search_term = request.GET['term']
        results = Sample.objects.filter(sample_name__icontains=search_term)
        return render_to_response('samples/sample_search_list.html', {'results': results})
    return render_to_response('samples/sample_search_list.html', {})

samples/urls.py 样本/ urls.py

urlpatterns = [ 
   path('', views.index, name='index'),
   path('samples/', views.sam, name='sam'),
   path('sample/<int:pk>', views.SampleDetailView.as_view(), name='sample-detail'),
   path('pi/', views.pi_table, name='pi_table'),
   path('pi/<int:pk>', views.pi_view, name='pi-detail'),
   path('samples/new', views.pi_new, name='pi_new'),
   path('samples/new_sample', views.sample_new, name='sample_new'),
   path('samples/', views.var, name='var'),
   path('samples/', views.search, name='search'),
]

samples/templates/samples/sample_search_list.html 样品/模板/样品/ sample_search_list.html

{% if results %}
   {% for result in results %}
      {{ result.sample_name }}
   {% endfor %}
{% else %}
    <h3 class='error'>There was a problem with your input.</h3>
{% endif %}

If anyone can help me identify the error I would greatly appreciate it. 如果有人可以帮助我确定错误,我将不胜感激。

I figured it out. 我想到了。 The name term has to be replaced with the name of the input form. 名称term必须替换为输入表单的名称。

search_term = request.GET['sample_search_box']

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

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