【问题标题】:Error: Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name错误:找不到“详细信息”的反向。 'detail' 不是有效的视图函数或模式名称
【发布时间】:2017-09-12 08:47:45
【问题描述】:

Django 版本:1.11.4 蟒蛇:3.6.1

我想在我的 html 文件中添加一行:

<a class="pure-button" href="{% url 'detail' id=post.id %}">Read More </a>

views.py:

def detail(request, id):
    try:
        post = Article.objects.get(id=int(id))
    except Article.DoesNotExist:
        raise Http404
    return render(request, 'post.html', {'post':post})

url.py:

from django.conf.urls import url,include
from django.contrib import admin
import article.views
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^(\d+)', article.views.detail),
    url(r'^', article.views.home),
]

我遇到了以下错误:

NoReverseMatch at /
Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name.
Request Method: GET
Request URL:    http://localhost:8000/
Django Version: 1.11.4
Exception Type: NoReverseMatch
Exception Value:    
Reverse for 'detail' not found. 'detail' is not a valid view function or pattern name.

谁能帮帮我?非常感谢!

【问题讨论】:

标签: django


【解决方案1】:
url(r'^(?P<id>\d+)/$', article.views.detail, name='detail'), 

将您的详细信息视图的网址更改为上方的网址

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 2020-12-24
    • 2020-05-30
    • 2018-01-25
    • 2020-12-01
    • 2019-06-27
    • 2021-05-18
    • 2018-11-19
    相关资源
    最近更新 更多