【问题标题】:Django haystack serving incorrect searched URLSDjango haystack 提供不正确的搜索 URL
【发布时间】:2012-04-12 03:46:34
【问题描述】:

我正在使用 haystack 搜索我的 django 网站,它完美地完成了这项工作。 但是在我的结果页面上,链接不起作用。 在我的模板中,我正在使用代码:

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

在我的 other/models.py 中,我包含了:

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

我的 urls.py 看起来像:

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"),
)

它应该链接到的 URL 是:

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

但它仍然链接到:

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

在 shell 中会发生这种情况:

>>> 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.

谢谢

【问题讨论】:

    标签: python django search django-haystack whoosh


    【解决方案1】:

    您应该在模型中定义 get_absolute_url() 方法。

    例如:

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

    这假设小部件详细视图的name of the url 是:'widget_detail'

    【讨论】:

    • 哪个型号?据我了解,我的 urls.py 指向搜索模板。我没有搜索应用程序(模型、视图等)。我按照 haystack 上的教程进行操作,他们就是这样做的。这是错的吗,我需要创建一个干草堆应用程序,即“startapp haystack”吗?谢谢
    • 将 get_absolute_url 方法添加到 haystack 正在搜索的模型中,您的问题就会消失。
    • 您的网址没有“名称”。请检查我的答案中的链接“网址名称”。当您“命名”您的网址时,它将起作用 B)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 1970-01-01
    • 2013-02-12
    • 2014-07-17
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多