繁体   English   中英

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

[英]How to implement search function in django?

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

在菜单中:

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

视图.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 %}

有几件事我想在这里添加..

1)我认为搜索结果应该是方法GET而不是POST。 您可以参考链接以查看何时使用 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) 视图.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 中有一些数据来获取一些搜索结果!

您将使用搜索选项的主 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 %}

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

看起来你需要使用for ,因为 books 是一个查询集

在模板中,尝试:

{% 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')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM