1

我想为我的 django 网站创建一个搜索功能。我按照此博客上的教程进行操作。当我搜索关键字时,它不会打印任何结果。我一直在努力完成这项工作,但没有成功。我怎样才能使这项工作更好?

楷模:

class Finb(models.Model):
     user=models.ForeignKey(User)
     title=models.CharField(max_length=250, unique=True)
     address=models.CharField(max_length=200)
     city=models.CharField(max_length=200)
     state=models.CharField(max_length=200)
     guide=models.TextField(max_length=1000)
     price=models.CharField(max_length=100)
     main_view=models.ImageField(upload_to="photos",null=True, blank=True, help_text='Optional.')
     side_view=models.ImageField(upload_to="photos",null=True, blank=True, help_text='Optional.')
     pub_date=models.DateTimeField()

     def __unicode__(self):
         return u'%s %s %s %s %s %s %s %s %s %s' % (self.title,self.address,self.city,self.state,self.guide,self.price,self.main_view,self.side_view,self.user,self.pub_date)
     @models.permalink
     def get_absolute_url(self):
         return ('meekapp.views.cripdetail', (),{'finb_id': self.id})

表格.py

import re

from django.db.models import Q

def normalize_query(query_string,
                    findterms=re.compile(r'"([^"]+)"|(\S+)').findall,
                    normspace=re.compile(r'\s{2,}').sub):
''' Splits the query string in invidual keywords, getting rid of unecessary spaces
    and grouping quoted words together.
    Example:

    >>> normalize_query('  some random  words "with   quotes  " and   spaces')
    ['some', 'random', 'words', 'with quotes', 'and', 'spaces']

'''
return [normspace(' ', (t[0] or t[1]).strip()) for t in findterms(query_string)] 

def get_query(query_string, search_fields):
''' Returns a query, that is a combination of Q objects. That combination
    aims to search keywords within a model by testing the given search fields.

'''
    query = None # Query to search for every search term        
    terms = normalize_query(query_string)
    for term in terms:
        or_query = None # Query to search for a given term in each field
        for field_name in search_fields:
            q = Q(**{"%s__icontains" % field_name: term})
            if or_query is None:
               or_query = q
            else:
                or_query = or_query | q
        if query is None:
           query = or_query
        else:
           query = query & or_query
     return query    

意见

def search(request):
    query_string = ''
    found_entries = None
    if ('q' in request.GET) and request.GET['q'].strip():
        query_string = request.GET['q']

        entry_query = get_query(query_string, ['title',])

    found_entries = Finb.objects.filter(entry_query).order_by('-pub_date')
    for result in found_entries:
        print result.title
        return render_to_response('search_results.html',
                      { 'query_string': query_string, 'found_entries': found_entries, 'result':result },
                      context_instance=RequestContext(request))

模板:

{% block content %}
  <div id="searchbody" class="wrapper">
    <div class="search-fed">      
      <form method="get" action=".">
     <label for="search"> </label> <input type="text" class="sfield" name="q" max_length="100"  />
     <input type="submit" class="searchput" value="Find" />        
       </form>
     </div>
    <span class="step-links">
       {% if query %}
        <h3>Your Search Results</h3>
  {% for result in found_entries %}

     <p>
         <p> <strong> <a href="{% url cripdetail result.object.id %}" >{{ result.title }}</a> </strong> </p>

    </p>
    {% empty %}
         <p>No results found.</p>
     {% endfor %}

   {% endif %}
 {% endblock %}

如果这看起来不整洁,有没有更好的方法来解决这个问题?

4

1 回答 1

2

为什么不使用现有的 python/django 包进行搜索?

Haystack 可以为您解决这个问题,让您只需要配置搜索选择搜索后端(solr、elasticsearch、..)

于 2012-12-03T08:14:36.380 回答