在django中使用绝对URL时,/ posts /中的NoReverseMatch

时间:2020-12-04 19:56:36

In my model.py, i have define a class:

在我的model.py中,我定义了一个类:

def get_absolute_url(self):
    return reverse("posts:detail", kwargs={"id":self.id})

In url.py(Project url)

在url.py(项目网址)

    urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^posts/', include("posts.urls", namespace='posts')),
]

In url.py(app url)

在url.py(app url)中

    urlpatterns = [
    url(r'^$', post_list),
    url(r'^create/$', post_create),
    url(r'^(?P<id>\d+)/$', post_detail, name='detail'),
    url(r'^update/$', post_update),
    url(r'^delete/$', post_delete),
]

In index.html

{% for obj in object_list %}
{% url "posts:detail" id=obj.id %}
<a href='{{ obj.get_absolute_url }}'>{{ obj.title }}</a><br/>
{{ obj.content }}<br/>
{{ obj.timestamp }}<br/>
{{ obj.updated_date }}<br/>
{{ obj.id }}<br/>

{% endfor %}

While reloading, it gives

在重装时,它给出了

Reverse for 'detail' with arguments '()' and keyword arguments '{'id': 1}' not found. 0 pattern(s) tried: []

1 个解决方案

#1


0  

The code for the URLpatterns and the reverse call look okay and works fine for me. I would check the view function has the argument id and that you are not loading a cached result. Also, it might be related to old pyc files being loaded, try delete them as @knbk suggested.

URLpatterns和反向调用的代码看起来没问题,对我来说很好。我会检查view函数是否有参数id,并且你没有加载缓存的结果。此外,它可能与正在加载的旧pyc文件有关,请尝试删除它们@ Knbk建议。

#1


0  

The code for the URLpatterns and the reverse call look okay and works fine for me. I would check the view function has the argument id and that you are not loading a cached result. Also, it might be related to old pyc files being loaded, try delete them as @knbk suggested.

URLpatterns和反向调用的代码看起来没问题,对我来说很好。我会检查view函数是否有参数id,并且你没有加载缓存的结果。此外,它可能与正在加载的旧pyc文件有关,请尝试删除它们@ Knbk建议。