问题
I successfully installed whoosh and made it work with Haystack. Things are working fine but I'm facing one problem which is; after searching for a keyword and it print out the results, when I click on the result(title), It won't redirect me to the page of the keyword I clicked on, it's just static. I tried adding a get_absolute_url method. Yet it's not working. What I'm I missing?
Models
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/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 %}
Urlconf
#url where the objects are posted.
(r'^find/$', findme),
#haystack url where you can search
(r'^search/', include('haystack.urls')),
Views:
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))
回答1:
In get_absolute_url
your URL is called findme
and you're giving it five parameters. In your URL configuration, the URL is there but:
- it is not called
findme
(i.e., you need to havename="findme"
), and - it also does not contain any parameters.
For example, the following URL has both a name and a named parameter (see documentation on URLs for more information):
(r'^articles/(?P<year>\d{4})/$', 'news.views.year_archive', name="news_year_archive"),
You need to create a similar URL with the parameters main_view
, side_view
, address
, city
and state
so Django can properly reverse the URL and provide an absolute URL for the model.
来源:https://stackoverflow.com/questions/11176625/haystack-with-whoosh-search-results-not-redirecting