2

Im using haystack to search my django website, it does this perfectly. However on my results page the links do not work. Within my template i am using the code:

<a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>

Within my other/models.py i have included:

def get_absolute_url(self):
    return urlresolvers.reverse('post', args=[self.pk])

My urls.py looks like:

from django.conf.urls.defaults import *
from dbe.other.models import *

urlpatterns = patterns('dbe.other.views',
(r"^(\d+)/$", "post"),
(r"^add_comment/(\d+)/$", "add_comment"),
(r"^delete_comment/(\d+)/$", "delete_comment"),
(r"^delete_comment/(\d+)/(\d+)/$", "delete_comment"),
(r"^month/(\d+)/(\d+)/$", "month"),
(r"", "main"),
)

The URL it should link to is:

http://127.0.0.1:8000/other/10/

But its still linking to:

http://127.0.0.1:8000/search/?q=searchterm

In the shell this happens:

>>> from other.models import Post
>>> inst = Post.objects.get(pk=1)
>>> inst.get_absolute_url()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/export/mailgrp4_a/sc10jbr/lib/python/django/utils/functional.py", line 55, in _curried
return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
File "/export/mailgrp4_a/sc10jbr/lib/python/django/db/models/base.py", line 887, in get_absolute_url
return settings.ABSOLUTE_URL_OVERRIDES.get('%s.%s' % (opts.app_label, opts.module_name), func)(self, *args, **kwargs)
File "/home/cserv2_a/soc_ug/sc10jbr/WWWdev/dbe/../dbe/other/models.py", line 18, in get_absolute_url
return urlresolvers.reverse('post', args=[self.pk])
File "/export/mailgrp4_a/sc10jbr/lib/python/django/core/urlresolvers.py", line 391, in reverse
*args, **kwargs)))
File "/export/mailgrp4_a/sc10jbr/lib/python/django/core/urlresolvers.py", line 337, in reverse
"arguments '%s' not found." % (lookup_view_s, args, kwargs))
NoReverseMatch: Reverse for 'post' with arguments '(1,)' and keyword arguments '{}' not found.

Thanks

Joseph Roberts
  • 569
  • 3
  • 10
  • 29

1 Answers1

6

You should define the get_absolute_url() method in your model.

For example:

from django.core import urlresolvers

class Widget(models.Model):
    # fields ...
    def get_absolute_url(self):
        return urlresolvers.reverse('widget_detail', args=[self.pk])

This assumes that the name of the url for widget detail view is: 'widget_detail'

jpic
  • 32,891
  • 5
  • 112
  • 113
  • Which model? As i understand it my urls.py points to the search template. I don't have a search app (models, views etc.) I followed the tutorial on haystack and this was how they did it. Is this wrong, do i need to create a haystack app i.e. "startapp haystack"? Thanks – Joseph Roberts Mar 28 '12 at 14:22
  • Add get_absolute_url method to the models that haystack is searching and your problem will be gone. – jpic Mar 28 '12 at 14:26
  • Your urls don't have "names". Please, check the link "name of the url" in my answer. When you will "name" your urls, it will work B) – jpic Mar 29 '12 at 11:32