在 Django 中编辑搜索视图
你好,我正在 django 工作,如何使用 Django 文档站点的搜索表单。以下是一些信息。 现在,有没有办法可以生成标题的部分搜索?例如,如果我想搜索一本名为“Apress”的书,但我没有输入整个单词,而是输入“ap”来获取 Apress。有没有办法实现这个解决方案?
from django.db.models import Q
from django.shortcuts import render_to_response
from models import Book
#views.py
def search(request):
query = request.GET.get('q', '')
if query:
qset = (
Q(title__icontains=query) |
Q(authors__first_name__icontains=query) |
Q(authors__last_name__icontains=query)
)
results = Book.objects.filter(qset).distinct()
else:
results = []
return render_to_response("books/search.html", {
"results": results,
"query": query
})
#search html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Search{% if query %} Results{% endif %}</title>
</head>
<body>
<h1>Search</h1>
<form action="." method="GET">
<label for="q">Search: </label>
<input type="text" name="q" value="{{ query|escape }}">
<input type="submit" value="Search">
</form>
{% if query %}
<h2>Results for "{{ query|escape }}":</h2>
{% if results %}
<ul>
{% for book in results %}
<li>{{ book|escape }}</l1>
{% endfor %}
</ul>
{% else %}
<p>No books found</p>
{% endif %}
{% endif %}
</body>
</html>
Hello I am working in django how to use a search form from Django documents site. Here are some information below.
Now, is there a way I can produce a part search for title? For example if I wanted to search for a book called "Apress", But instead of typing the whole word I just wrote "ap" to get Apress. Is there a way how to achieve this solution?
from django.db.models import Q
from django.shortcuts import render_to_response
from models import Book
#views.py
def search(request):
query = request.GET.get('q', '')
if query:
qset = (
Q(title__icontains=query) |
Q(authors__first_name__icontains=query) |
Q(authors__last_name__icontains=query)
)
results = Book.objects.filter(qset).distinct()
else:
results = []
return render_to_response("books/search.html", {
"results": results,
"query": query
})
#search html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
<title>Search{% if query %} Results{% endif %}</title>
</head>
<body>
<h1>Search</h1>
<form action="." method="GET">
<label for="q">Search: </label>
<input type="text" name="q" value="{{ query|escape }}">
<input type="submit" value="Search">
</form>
{% if query %}
<h2>Results for "{{ query|escape }}":</h2>
{% if results %}
<ul>
{% for book in results %}
<li>{{ book|escape }}</l1>
{% endfor %}
</ul>
{% else %}
<p>No books found</p>
{% endif %}
{% endif %}
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想在字段的开头进行匹配,则可以使用
startswith
或istartswith
(如果您希望区分大小写)。您现在使用的icontains
即使在字符串内也允许匹配,即。 “arry”将匹配“Harry”。虽然startswith
将允许 'Har' 匹配 'Harry',但不允许 'ArHarHar'If you want to match on the beginning of a field, you can use
startswith
oristartswith
if you want it to be case sensitive.icontains
which you are using now will allow matches even within strings, ie. 'arry' will match 'Harry'. Whilestartswith
will allow 'Har' to match 'Harry', but not 'ArHarHar'