[英](Haystack, Woosh, Django) Search Results won't show up
So I recently just added user search to to my site. 因此,我最近将用户搜索添加到了我的网站。 However, when I look up by location or discipline, nothing shows up. 但是,当我按位置或学科查找时,什么也没有显示。 User Profile has a link to user. 用户个人资料具有指向用户的链接。 For example, I have a user profile object with Los Angeles and a discipline of Journalism. 例如,我有一个洛杉矶的用户个人资料对象和一个新闻学学科。 But if I search, Los Angeles, nothing pops up. 但是,如果我搜索洛杉矶,什么都不会弹出。 Even if I check the box that says "Search in User Profiles". 即使我选中“在用户配置文件中搜索”框,也是如此。
views.py views.py
def search(request):
context = RequestContext(request)
render_to_response('search.html', context )
models.py 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)
Haystack Specific files 干草堆特定文件
search_indexes.py 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 search.html
{% extends 'base.html' %}
{% block content %}
<h2>Search</h2>
<form method="get" action=".">
<table>
{{ form.as_table }}
<tr>
<td> </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 }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% 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 userprofile_text.txt
{{ object.title }}
{{ object.user.get_full_name }}
{{ object.body }}
Your problem has to do with your index_queryset. 您的问题与您的index_queryset有关。 Change 更改
def index_queryset(self, using=None):
return self.get_model().objects.filter(location=location)
to 至
def index_queryset(sef, using=None):
return self.get_model().objects.filter()
and then rebuild your index. 然后重建索引。
index_queryset
is your default QuerySet, this is what your search will filter to give you your results. index_queryset
是您的默认QuerySet,这是您的搜索将过滤以提供给您结果的内容。 What you had before was making it so your default QuerySet filtered by what was essentially location=Null
, leaving you with 0 items in your QuerySet. 以前是进行此操作的,所以您的默认QuerySet被本质上是location=Null
过滤,在QuerySet中留下了0个项目。
Source at the django-haystack docs django-haystack文档来源
If you still have troubles after this please feel free to post a comment. 如果您在此之后仍然遇到困难,请随时发表评论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.