我做了一个搜索栏,我想它搜索的标题,这是在网站上。在键入之前,什么都不显示,但每当我键入一个标题时,所有标题都会出现。如何解决这个问题?你知道吗
索引.html
def index(request):
query = request.GET.get('srh')
if query:
target1 = Destination.objects.filter(title__icontains=query)
target1 = a, b= [Destination() for __ in range(2)]
a.img = 'Article.jpg'
b.img = 'Micro Tasks.jpeg'
a.title = 'Article Writing'
b.title = 'Micro Tasks'
context = {'target1': target1}
return render(request, 'index.html', context)
else:
return render(request, 'index.html')
视图.py
<form class="love" method="GET" action="">
{% csrf_token %}
<input type="text" placeholder='Search..' name="srh" value="{{request.GET.srh}}"> <br>
<button type="submit" class="btn btn-danger"> Search </button>
</form>
<div>
{% for dest1 in target1 %}
{% if dest1 %}
<div>
<a href="{{baseUrl}}/{{dest1.img}}">
<img src="{{hiUrl}}/{{dest1.img}}" alt="" />
<h3>{{dest1.title}}</h3>
</a>
</div>
{% endif %}
{%endfor%}
</div>
objects.filter
从数据库中读取,但数据库中没有对象。你知道吗这就足够了:
但当数据库为空时,它当然不会返回任何对象。你知道吗
.py代码:
和.html
相关问题 更多 >
编程相关推荐