2015-06-27 158 views
1

因此,我最近剛剛將用戶搜索添加到我的網站。但是,當我按地點或紀律查找時,什麼也沒有顯示出來。用戶配置文件有一個用戶鏈接。例如,我有一個洛杉磯用戶配置文件對象和一個新聞學科。但是如果我搜索洛杉磯,什麼都不會彈出。即使我選中了「在用戶配置文件中搜索」框。(Haystack,Woosh,Django)搜索結果不顯示

views.py

def search(request): 
    context = RequestContext(request) 
    render_to_response('search.html', context) 

models.py

class UserProfile(models.Model): 
#this line is required. Links MyUser to a User Model 
user = models.OneToOneField(User, related_name ="profile") 

#Additional Attributes we wish to include 
date_of_birth = models.FloatField(blank=False) 
phone = models.CharField(max_length=10, blank = True) 
city = models.CharField(max_length=40, blank = True) 
state = models.CharField(max_length=2, blank = True) 
zipCode = models.CharField(max_length=5, blank = True) 
admin = models.BooleanField(default=False, blank = True) 
mentor = models.BooleanField(default=False, blank = True) 
mentee = models.BooleanField(default=False, blank = True) 
# profilepicture = models.ImageField() 
#is_staff = True 
tagline = models.CharField(max_length=70, blank = True, default="Let's do this!") 
interests = models.ManyToManyField(Interest, related_name="interest", symmetrical = False) 
primaryDiscipline = models.ForeignKey(Discipline, default=False, blank = True) 
addtlDisciplines = models.ManyToManyField(Discipline, related_name="disciplines", symmetrical=False) 

草堆具體文件

search_indexes.py

import datetime 
from haystack import indexes 
from myapp.models import UserProfile 


class UserIndex(indexes.SearchIndex, indexes.Indexable): 
    text = indexes.CharField(document=True, use_template=True) 
    primaryDiscipline = indexes.CharField(model_attr='primaryDiscipline') 
    location = indexes.CharField(model_attr='city') 

    def get_model(self): 
     return UserProfile 

    def index_queryset(self, using=None): 
     return self.get_model().objects.filter(location=location) 

search.html

{% extends 'base.html' %} 

{% block content %} 
    <h2>Search</h2> 

    <form method="get" action="."> 
     <table> 
      {{ form.as_table }} 
      <tr> 
       <td>&nbsp;</td> 
       <td> 
        <input type="submit" value="Search"> 
       </td> 
      </tr> 
     </table> 

     {% if query %} 
      <h3>Results</h3> 

      {% for result in page.object_list %} 
       <p> 
        <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a> 
       </p> 
      {% empty %} 
       <p>No results found.</p> 
      {% endfor %} 

      {% if page.has_previous or page.has_next %} 
       <div> 
        {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %} 
        | 
        {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %} 
       </div> 
      {% endif %} 
     {% else %} 
      {# Show some example queries to run, maybe query syntax, something else? #} 
     {% endif %} 
    </form> 
{% endblock %} 

userprofile_text.txt

{{ object.title }} 
{{ object.user.get_full_name }} 
{{ object.body }} 
+0

簡單的事情第一 - 你運行命令來創建索引? –

+0

是的,我做到了。我運行了reindex,並更新索引。 – Tai

回答

0

你的問題與你的index_queryset做。 更改

def index_queryset(self, using=None): 
    return self.get_model().objects.filter(location=location) 

def index_queryset(sef, using=None): 
    return self.get_model().objects.filter() 

,然後重建索引。

index_queryset是您的默認QuerySet,這是您的搜索將過濾以向您提供結果的內容。你之前做的是讓你的默認QuerySet過濾掉本質上爲location=Null的內容,在你的QuerySet中留下0項。

Source at the django-haystack docs

如果您仍然有麻煩在這之後請隨時發表評論。