简体   繁体   中英

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. 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.

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.

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:

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. Here is a smattering of my code/files.

samples/models.py

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

samples/templates/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.

samples/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

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

{% 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.

search_term = request.GET['sample_search_box']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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