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})
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.
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'), #...
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.
When you meet the NoReverseMatch error, just check your URL mapping pattern carefully.
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 %}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With