在我的基本模板中,我有这个搜索栏。当我搜索某些东西时,它实际上并没有返回任何东西。我不确定网址是否正确,因为我希望它能在每个网页上运行。
在基本导航栏中,这是代码:
<form class="navbar-form navbar-left" target="_self">
<div class="form-group">
<label class="control-label" for="search-field">
<i class="glyphicon glyphicon-search"></i></label>
<input type="text/submit" value="{{request.GET.q}}" name="q"
class="form-control search-field"
action={% url 'search_posts' %}
placeholder="Search Airline or Aircraft" id="search-field" />
</div>
</form>
Urls.py
url(r'^$', search_posts, name='search_posts'),
Views.py
def search_posts(request):
aircraft = Aircraft.objects.all()
airline = Airline.objects.all()
query = request.GET.get("q")
if query:
aircraft = aircraft.filter(
Q(name__icontains=query) |
Q(description__icontains=query)
).distinct()
return render(request, 'search_post.html', {'aircraft': aircraft})
是否可以将两个模型组合成一个变量?我有Aircraft
和Airline
。这有用吗?
queryset_list = Aircraft.objects.all() + Airline.objects.all()
Views.py已更新
def search_posts(request):
aircraft = Aircraft.objects.all()
airline = Airline.objects.all()
query = request.GET.get("q")
aircraft = aircraft.filter(
Q(name__icontains=query) |
Q(description__icontains=query)).distinct()
airline = airline.filter(
Q(name__icontains=query) |
Q(description__icontains=query)).distinct()
return render(request, 'search_post.html', {'aircraft': aircraft,'airline': airline })
search_post.html
<div class="team-boxed">
<div class="container">
<div class="row aircraft">
{% for aircraft in aircraft %}
<div class="col-lg-offset-0 col-md-4 col-sm-3 item">
<div class="box"><img src="{{ aircraft.image.url }}" />
<h3 class="name"><a href="{{ aircraft.get_absolute_url }}">{{ aircraft.name }}</a></h3>
<h4><em>Range: {{ aircraft.maximum_range }} NM</em></h4>
<h4><em> Passengers: {{ aircraft.passengers }}</em></h4>
<h4><em> Speed: {{ aircraft.cruising_speed }} Kt</em></h4>
</div>
</div>
{% endfor %}
</div>
</div>
</div>
答案 0 :(得分:1)
由于HTML input
元素没有action
属性,请从action={% url 'search_posts' %}
移除<input>
并将其放在<form>
内,如下所示:
<form action={% url 'search_posts' %} method="GET" ...>
同时从输入中删除type="text/submit"
并将其替换为text
(为了向用户显示一个文本输入框来编写查询 - 感谢Alasdair为此 - 避风港&#t; t看到了),像这样:
<input type="text" ...>
所以,form
应如下所示:
<form method="GET" action={% url 'search_posts' %} class="navbar-form navbar-left">
<div class="form-group">
<label class="control-label" for="search-field">
<i class="glyphicon glyphicon-search"></i></label>
<input type="text" value="{{request.GET.q}}" name="q" class="form-control search-field" placeholder="Search Airline or Aircraft" id="search-field" />
</div>
</form>
关于你问题的其他部分,为了结合两个(或更多)QuerySet
(我无法想象你为什么要这样做),这里是:
qs = list(Aircraft.objects.all()) + list(Airline.objects.all())
最后在search_post.html
执行此操作:
{% if aircraft.exists %}
<div class="team-boxed">
<div class="container">
<div class="row aircraft">
{% for aircraft in aircraft %}
<div class="col-lg-offset-0 col-md-4 col-sm-3 item">
<div class="box"><img src="{{ aircraft.image.url }}"></div>
<h3 class="name"><a href="{{ aircraft.get_absolute_url }}">{{ aircraft.name }}</a></h3>
<h4><em>Range: {{ aircraft.maximum_range }} NM</em></h4>
<h4><em> Passengers: {{ aircraft.passengers }}</em></h4>
<h4><em> Speed: {{ aircraft.cruising_speed }} Kt</em></h4>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}
{% if airline.exists %}
<div class="team-boxed">
<div class="container">
<div class="row airline">
{% for airline in airline %}
<div class="col-lg-offset-0 col-md-4 col-sm-3 item">
<div class="box"><img src="{{ airline.image.url }}"></div>
<h3 class="name"><a href="{{ airline.get_absolute_url }}">{{ airline.name }}</a></h3>
<h4><em>Range: {{ airline.maximum_range }} NM</em></h4>
<h4><em> Passengers: {{ airline.passengers }}</em></h4>
<h4><em> Speed: {{ airline.cruising_speed }} Kt</em></h4>
</div>
{% endfor %}
</div>
</div>
</div>
{% endif %}