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> </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
{{ object.title }}
{{ object.user.get_full_name }}
{{ object.body }}
簡單的事情第一 - 你運行命令來創建索引? –
是的,我做到了。我運行了reindex,並更新索引。 – Tai