Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django NoReverseMatch

Tags:

python

django

I can't understand what's wrong? I tried to make a detail page of one item, like in example on http://tutorial.djangogirls.org/en/extend_your_application/index.html and It doesn't work in my project, but In exercises everything was good.

Error message: NoReverseMatch at /

Reverse for 'events.views.event_detail' with arguments '()' and keyword arguments '{u'pk': 3}' not found. 1 pattern(s) tried: ['$event/(?P<pk>[0-9]+)/$']

HTML(fragment)

<div class="col-xs-6"><a class="btn btn-primary" href="{% url 'events.views.event_detail' pk=event.pk %}">Read more</a></div>
         </div>

settings.py

ROOT_URLCONF = 'mysite.urls' 

app urls.py

from django.conf.urls import include, url
from . import views

urlpatterns = [
    url(r'^$', views.events_list),
    url(r'^event/(?P<pk>[0-9]+)/$', views.event_detail),
]

app views.py

from django.shortcuts import render, get_object_or_404
from django.utils import timezone
from .models import Event

def events_list(request):
    events = Event.objects.filter(published_date__lte=timezone.now()).order_by('published_date')
    return render(request, 'events/events_list.html', {'events': events})

def event_detail(request, pk):
    event = Event.objects.get(pk=pk)
    return render(request, 'events/event_detail.html', {'event': event})
like image 595
Andrey Nadosha Avatar asked Jul 20 '15 18:07

Andrey Nadosha


People also ask

What is noreversematch error in Django?

When is it unable to find a URL that matches your specified url_name, it returns NoReverseMatch Error. Basically, you get this error when Django is unable to find a Reverse Match for the URL name you have mentioned in your template.

Why do I get a reverse match error in Django?

Basically, you get this error when Django is unable to find a Reverse Match for the URL name you have mentioned in your template. from django.urls import path from . import views app_name = 'polls' urlpatterns = [ #... path ('articles/<int:year>/', views.year_archive, name='news-year-archive'), #...

What is this Django error trying to tell me?

First lets see what this error is trying to tell developer. Django is trying to find a URL with name 'url_name' and it is not able to find any pattern match. Application traverses the urls.py file from top to bottom and match the pattern.

What should I do when I meet the noreversematch error?

When you meet the NoReverseMatch error, just check your URL mapping pattern carefully.


1 Answers

You haven't shown your mysite.urls, but from the error message it looks like you have done something like this:

(r'^events/$', include('events.urls')),

You need to drop the terminating $, since that means the end of the regex; nothing can match after that. It should be:

(r'^events/', include('events.urls')),

Note that you should also give your event URLs names, to make it easier to reference:

url(r'^$', views.events_list, name='events_list'),
url(r'^event/(?P<pk>[0-9]+)/$', views.event_detail, name='event_detail'),

so that you can now do:

{% url 'event_detail' pk=event.pk %}
like image 74
Daniel Roseman Avatar answered Oct 27 '22 04:10

Daniel Roseman