Django2.2.1利用表单实现搜索功能
项目路径/app文件夹/urls.py
urlpatterns = [
...
path('search_form/', views.search_form, name='search_form'),
path('search/', views.search, name='search'),
]
项目路径/app文件夹/views.py
def search_form(request):
return render_to_response('polls/search_form.html')
def search(request):
if 'q' in request.GET:
message = 'You searched for: %r' % request.GET['q']
else:
message = 'You submitted an empty form.'
return HttpResponse(message)
项目路径/app文件夹/templates/app文件夹/search_form.html(模板)
<html>
<head>
<title>Search</title>
</head>
<body>
<form action="http://localhost:8000/polls/search/" method="get">
<input type="text" name="q">
<input type="submit" value="Search">
</form>
</body>
</html>