1

Django version: 1.11.4 Python: 3.6.1

I want to add a line to my html file:

<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),
]

I met the following errors:

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.

Who can help me? Many thanks!

stelios
  • 2,679
  • 5
  • 31
  • 41
Jetliu
  • 11
  • 1
  • 3
  • Well the errors correct... you don't have a url named detail. – Sayse Sep 12 '17 at 08:51
  • Possible duplicate of [What is a NoReverseMatch error, and how do I fix it?](https://stackoverflow.com/questions/38390177/what-is-a-noreversematch-error-and-how-do-i-fix-it) – Sayse Sep 12 '17 at 08:52

1 Answers1

3
url(r'^(?P<id>\d+)/$', article.views.detail, name='detail'), 

change the url of your details view to the upper one

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
Exprator
  • 26,992
  • 6
  • 47
  • 59