我成功安装了 whoosh 并使它与 Haystack 一起工作。一切正常,但我面临一个问题:搜索关键字并打印出结果后,当我点击结果(标题)时,它不会将我重定向到我点击的关键字的页面,它只是静态的。我尝试添加一个 get_absolute_url 方法。然而它不起作用。我错过了什么?
楷模
class Meek(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)
main_view=models.ImageField(upload_to="photos",blank=True, null=True)
side_view=models.ImageField(upload_to="photos",blank=True, null=True)
pub_date=models.DateTimeField()
def __unicode__(self):
return self.title
@models.permalink
def get_absolute_url(self):
return ('findme', (), {
'main_view': self.main_view,
'side_view': self.side_view,
'address': self.address,
'city': self.city,
'state': self.state})
搜索/search.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 %}
网址配置
#url where the objects are posted.
(r'^find/$', findme),
#haystack url where you can search
(r'^search/', include('haystack.urls')),
意见:
def findme(request):
extra_data_context={}
#if there's nothing in the field do nothing.
if request.method=="POST":
form=MeekForm(request.POST, request.FILES)
if form.is_valid():
data=form.cleaned_data
newmeeks=Meek(
user=request.user,
pub_date=datetime.datetime.now(),
title=data['title'],
main_view=request.FILES['main_view'],
side_view=request.FILES['side_view'],
address=data['address'],
city=data['city'],
state=data['state'])
newmeeks.save()
extra_data_context.update({'MeekForm':form})
else:
form = MeekForm()
extra_data_context.update({'MeekForm':form})
extra_data_context.update({'Meeks':Meek.objects.filter(user=request.user)})
return render_to_response('postme.html',extra_data_context,context_instance=RequestContext(request))