如何在django中实现搜索功能?

时间:2016-06-24 05:24:39

标签: python django django-views

我在数据库中有很多书。搜索数据库不会返回任何内容(空查询集)。

在菜单中:

<form name="myform" method="POST" action="{% url 'search' %}">  
    {% csrf_token %}
    <input type="text" name="search" placeholder="Search" />
</form> 

views.py,

def search(request):        
    if request.method == 'POST':      
        book_name =  request.POST.getlist('search')      
        try:
            status = Add_prod.objects.filter(bookname__icontains=book_name)
            #Add_prod class contains a column called 'bookname'
        except Add_prod.DoesNotExist:
            status = None
        return render(request,"search.html",{"books":status})
    else:
        return render(request,"search.html",{})

我的模板文件

{% if books %}
    <a href="{% url 'buy_book' pk=books.pk %}">
    <!--Upon successful search book image with hyperlink appears -->
    <img src="{{books.image.url}}" alt="No Image"></a>
    <p>{{books.bookname}}</p>
    <p>Rs.{{books.price}}</p>   
{% endif %}

3 个答案:

答案 0 :(得分:5)

我想在这里添加几件事情。

1)我认为搜索结果应该是方法GET而不是POST。您可以参考this链接查看何时使用POST以及何时使用GET

<form name="myform" method="GET" action="{% url 'search' %}"> 
// see if you need csrf_token here when this is GET request. Your homework

2)views.py

def search(request):        
    if request.method == 'GET': # this will be GET now      
        book_name =  request.GET.get('search') # do some research what it does       
        try:
            status = Add_prod.objects.filter(bookname__icontains=book_name) # filter returns a list so you might consider skip except part
        return render(request,"search.html",{"books":status})
    else:
        return render(request,"search.html",{})

3)django模板现在应该是

{% if books %}
{% for each_book in books %} // books is list here so pick each element using for loop 
    <a href="{% url 'buy_book' pk=each_book.pk %}">
    <!--Upon successful search book image with hyperlink appears -->
    <img src="{{each_book.image.url}}" alt="No Image"></a>
    <p>{{each_book.bookname}}</p>
    <p>Rs.{{each_book.price}}</p>   
{% endfor %}
{% endif %}

这应该可以正常工作。

还要确保在db中有一些数据可以在搜索时获取一些结果!

答案 1 :(得分:2)

您将在其中使用搜索选项的主HTML中的表单应如下所示:-

<form name="myform" method="GET" action="{% url 'search' %}">  
<input type="text" name="search" placeholder="Search" /></form> 

您需要将views.py编辑为

def search(request):
    if request.GET.get('myform'): # write your form name here      
        book_name =  request.GET.get('myform')      
        try:
            status = Add_prod.objects.filter(bookname__icontains=book_name)
            return render(request,"search.html",{"books":status})
        except:
            return render(request,"search.html",{'books':status})
   else:
        return render(request, 'search.html', {'books':status})

您的Django模板应如下所示:-

   {% if books %}
       {% for each_book in books %} 
           <a href="{% url 'buy_book' pk=each_book.pk %}">
           <img src="{{each_book.image.url}}" alt="No Image"></a>
           <p>{{each_book.bookname}}</p>
           <p>Rs.{{each_book.price}}</p>   
       {% endfor %}
   {% endif %}

这样做,到那时您的代码就可以正常工作了……

答案 2 :(得分:1)

您似乎需要使用for,因为图书是一个查询集

在模板中,尝试:

{% if books %}
{% for book in books %}
    <a href="{% url 'buy_book' pk=book.pk %}">
    <!--Upon successful search book image with hyperlink appears -->
    <img src="{{book.image.url}}" alt="No Image"></a>
    <p>{{book.bookname}}</p>
    <p>Rs.{{book.price}}</p>   
{% endfor %}
{% endif %}

在你的意见中:

相反:

book_name =  request.POST.getlist('search')

尝试:

book_name =  request.POST.get('search')