The implementation of the search function is that the user enters the content to be searched on the front page, then passes through the URL to the backend, the backend view function finds out the specified object from the database, and then passes the procedure to the front page.
Here we mainly talk about the lookup process in the View function:
view.py
From django.db.models import Q
#url to 127.0.0.1:8000/course/list/?keywords=django
def MyApp (request):
if Request.method = = ' GET ':
keywords = Request. Get.get (' keywords ', ') #得到搜索关键词 ' Django '
course_list = Course.objects.filter (Q (name__icontains=keywords) | Q (detail__icontains=keywords))
return render (Request, ' course-list.html ', {' course_list ': course_list})
This is where Name__icontains gets the object containing keywords in name, and for more usage of object_filter (), see my previous blog: objects.filter () usage
Using the Q () method to implement multiple query conditions, for more information about Q (), see my previous blog: using the Q () method for querying
And in the front-end code implementation, mainly through the JS code to achieve click Search function, here no longer repeat (in fact, I will not-_-), the code is as follows:
//Top Search Bar Search method function Search_click () {var type = $ (' #jsSelectOption '). attr (' Data-value '),
Keywords = $ (' #search_keywords '). Val (), Request_url = ";
if (keywords = = "") {return} if (type = = "Course") {Request_url = "/course/list?keywords=" +keywords }else if (type = = "Teacher") {Request_url = "/org/teacher/list?keywords=" +keywords}else if (type = = "org") {Request_url = "/org/list?keywords=" +keywords} window.location.href = Request_url}