(Haystack,Woosh,Django)搜索结果将不会显示

时间:2015-06-27 22:13:00

标签: python django django-haystack

所以我最近刚刚将用户搜索添加到我的网站。但是,当我按位置或纪律查看时,什么都没有显示出来。用户个人资料具有指向用户的链接。例如,我有洛杉矶的用户档案对象和新闻学科。但是,如果我搜索洛杉矶,没有任何东西出现。即使我选中“在用户配置文件中搜索”框。

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)

Haystack特定文件

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 }}

1 个答案:

答案 0 :(得分: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

如果您在此之后仍有麻烦,请随时发表评论。