我有简单的搜索应用程序我如何按类别搜索使用<select><option></select>
结果 :
results = Adv.objects.filter(title__icontains=query).filter(cate__name__icontains=query)
这是代码:
楷模
class Category(models.Model):
slug = models.SlugField(unique=True)
name = models.CharField(max_length=60)
class Adv(models.Model):
title = models.CharField(max_length=60)
pub_date = models.DateTimeField(auto_now_add=True)
namecar = models.CharField(max_length=60)
cate = models.ForeignKey(Category)
看法
def search(request):
if 'q' in request.GET:
query = request.GET['q']
results = Adv.objects.filter(title__icontains=query).filter(cate__name__icontains=query)
# print results.query
else:
query = ""
results = None
template = loader.get_template('search/search1.html')
context = Context({ 'query': query, 'results': results, 'city_list': ChoiceCity.objects.all() })
response = template.render(context)
return HttpResponse(response)
网址
(r'^search1/$', 'car.search.views.search'),
模板
<form method="get" action="/search1/">
<p><label for="id_q">Search:</label>
<input type="text" name="q" id="id_q" value="{{ query }}" />
<input type="submit" value="Submit" /></p>
</form>