0

I'm trying to implement generic editing views as shown here:

I started with the CreateView which renders and submits data correctly. However, I am getting an error when I tries to use reverse() to return to the detail view page for the new object.

Here is my Error message:

NoReverseMatch at /work/clients/create/ Reverse for 'ClientDetailView' with arguments '('14',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []

Here is how I defined get_absolute_url() in my model:

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

My view is called ClientDetailView. I'm not sure what other information would be helpful.

Here is class ClientDetailView:

class ClientDetailView(generic.DetailView):    
    model = Client
    template_name = 'work/client_detail.html'`

and here is url() from urls.py:

url(r'^clients/(?P<pk>[0-9]+)/$', views.ClientDetailView.as_view(), name='clients_detail'),`

Can anyone explain what I am doing wrong?

Dhia
  • 10,119
  • 11
  • 58
  • 69
Solomon Bothwell
  • 1,004
  • 2
  • 12
  • 21

2 Answers2

0

I solved my own problem. I had to add the namespace to the reverse() method:

return reverse('work:clients_detail', kwargs={'pk': self.pk})

I would appreciate if someone else could explain why I needed to do this.

Here is the my complete urls.py: from django.conf.urls import url

from . import views

app_name = 'work'
urlpatterns = [
    url(r'^work_orders/$', views.WorkOrdersIndexView.as_view(), name='quotes_index'),
    url(r'^work_orders/(?P<pk>[0-9]+)/$', views.WorkOrdersDetailView.as_view(), name='work_orders_detail'),
    url(r'^quotes/$', views.QuotesIndexView.as_view(), name='quotes_index'),
    url(r'^quotes/(?P<pk>[0-9]+)/$', views.QuotesDetailView.as_view(), name='quotes_detail'),
    url(r'^project/(?P<pk>[0-9]+)/$', views.ProjectDetailView.as_view(), name='project_detail'),
    url(r'^project/create/$', views.ProjectCreateView.as_view(), name='project_create'),
    url(r'^project/(?P<pk>[0-9]+)/update/$', views.ProjectUpdateView.as_view(), name='project_update'),
    url(r'^project/(?P<pk>[0-9]+)/delete/$', views.ProjectDeleteView.as_view(), name='project_delete'),
    url(r'^clients/$', views.ClientView.as_view(), name='client_index'),
    url(r'^clients/(?P<pk>[0-9]+)/$', views.ClientDetailView.as_view(), name='clients_detail'),
    url(r'^clients/create/$', views.ClientCreateView.as_view(), name='client_create'),
    url(r'^clients/(?P<pk>[0-9]+)/update/$', views.ClientUpdateView.as_view(), name='clients_update'),
    url(r'^clients/(?P<pk>[0-9]+)/delete/$', views.ClientDeleteView.as_view(), name='clients_delete'),


]
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
Solomon Bothwell
  • 1,004
  • 2
  • 12
  • 21
  • 1
    Without your urls.py file(s) it is hard to say. Also it could be that the correction from `reverse('ClientDetailView'...` to `reverse('clients_detail',` would have been enough. – tobltobs Jun 11 '16 at 02:17
  • @tobltobs i added my urls.py file to to the main comment. – Solomon Bothwell Jun 11 '16 at 02:43
  • Do you include this file somewhere else, for eg. in a core app? Did you test with `reverse('clients_detail',..`? – tobltobs Jun 11 '16 at 03:07
0

actually you are trying to reverse the view, instead of ClientDetailView use url name clients_detail

Rahul Vivek
  • 196
  • 1
  • 3