Django haystack服务错误的搜索URL

2024-10-02 02:37:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我用干草堆搜索我的django网站,它做得很完美。 但是在我的结果页面链接不起作用。 在我的模板中,我使用了以下代码:

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

在我的另一半/模型.py我包括:

^{pr2}$

我的网址.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.

谢谢


Tags: djangoinpyurlgetmodelslinecomment
1条回答
网友
1楼 · 发布于 2024-10-02 02:37:49

您应该在模型中定义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])

这假设widget detail视图的name of the url是:“widget_detail”

相关问题 更多 >

    热门问题