在Djang中使用get\u absolute\u url时,NoReverseMatch

2024-09-26 22:50:00 发布

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

错误跟踪:

NoReverseMatch at /search/
    Reverse for '/about/murder-in-the-curtain' not found. '/about/murder-in-the-curtain' is not a valid view function or pattern name.
    Request Method: GET
    Request URL:    http://127.0.0.1:8000/search/?q=mur
    Django Version: 1.11.6
    Exception Type: NoReverseMatch
    Exception Value:    
    Reverse for '/about/murder-in-the-curtain' not found. '/about/murder-in-the-curtain' is not a valid view function or pattern name.

URL配置:

 url(r'^about/(?P<slug>[-\w]+)$', about_pages, name="about_pages")

型号:

class Book(models.Model):
        slug = models.SlugField(default = "")

        def get_absolute_url(self):
            reverse("about_pages", kwargs = {"slug" : self.slug})

模板:

{% for result in results %}
<a href = {% url result.get_absolute_url %}>{{result.name}}</a>
{% endfor %}

我的正则表达式还好吗? 我觉得它正确地生成了url,但是没有在URLConf中找到匹配的模式来导航到一个视图。你知道吗


Tags: thenameinurlforsearchnotpages
2条回答

get_absolute_url方法反转url并返回它,因此您不必对其使用{% url %}标记。将模板更改为:

<a href="{{ result.get_absolute_url }}">{{result.name}}</a>
<a href = {% url result.get_absolute_url %}>{{result.name}}</a>

你不应该这样使用它,因为它是一个Book方法,而不是url路径

所以你应该写在这里

<a href = {{ result.get_absolute_url }}>{{result.name}}</a>

相关问题 更多 >

    热门问题