我成功安装了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配置
#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))
答案 0 :(得分:0)
在get_absolute_url
中,您的网址称为findme
,您将为其提供五个参数。在您的URL配置中,URL存在但是:
findme
(即,您需要name="findme"
),例如,以下URL同时具有名称和命名参数(有关详细信息,请参阅documentation on URLs):
(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive', name="news_year_archive"),
您需要使用参数main_view
,side_view
,address
,city
和state
创建类似的网址,以便Django可以正确撤消网址和提供模型的绝对URL。