错误追踪:
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(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中的匹配模式来导航到视图。
答案 0 :(得分:1)
get_absolute_url
方法会反转网址并将其返回,因此您不必使用{% url %}
标记。将模板更改为:
<a href="{{ result.get_absolute_url }}">{{result.name}}</a>
答案 1 :(得分:0)
<a href = {% url result.get_absolute_url %}>{{result.name}}</a>
您不应那样使用它,因为它是Book的方法,而不是url路径,
所以你应该在这里写
<a href = {{ result.get_absolute_url }}>{{result.name}}</a>