RubyNoob
RubyNoob

Reputation: 547

NoReverseMatch error after updating form in Django

from sites/models.py

def get_absolute_url(self):
    return reverse('site-detail', kwargs={'pk':self.pk})

sites/urls.py

from django.conf.urls import url
from sites.views import SiteCreate, SiteUpdate, SiteDelete, IndexView, TestedView, DetailView, ApproveSite

app_name = 'sites'
urlpatterns = [
   url(r'^$', IndexView.as_view(), name='site-list'),
   url(r'add/$', SiteCreate.as_view(), name='site-add'),
   url(r'(?P<pk>[0-9]+)/update/$', SiteUpdate.as_view(), name='site-update'),
   url(r'delete/$', SiteDelete.as_view(), name='delete'),    
   url(r'tested/$', TestedView.as_view(), name='site-tested'),
   url(r'(?P<pk>[0-9]+)/$', DetailView.as_view(), name='site-detail'),
   url(r'approve/$', ApproveSite.as_view(), name='approve')
]

from sites/views.py

class DetailView(generic.DetailView):
    model = Site
    template_name = "sites/detail.html"
    context_object_name = "site"

sites/forms.py

from django import forms
from sites.models import Site, Category

class SiteForm(forms.ModelForm):
    class Meta:
        model = Site
        fields = '__all__'
        exclude = ('Status',)

When I update a site using the form from my site update view, I want it to take me to the site detail view, but it throws a NoReverseMatch error. Specifically:

NoReverseMatch at /sites/1/update/
Reverse for 'site-detail' with arguments '()' and keyword arguments '{'pk': 1}' not found. 0 pattern(s) tried: []

Why is this error happening and how can I get rid of it so that my app shows the detail view for my site record, after my form is submitted?

Upvotes: 1

Views: 267

Answers (1)

Alasdair
Alasdair

Reputation: 308939

You have app_name = 'sites' in your app's urls.py, so you should include the sites namespace when reversing the url:

def get_absolute_url(self):
    return reverse('sites:site-detail', kwargs={'pk':self.pk})

Upvotes: 2

Related Questions