AlliDeacon
AlliDeacon

Reputation: 1495

URL Tags to link from another installed app in Django

Running Django 3.0.7 on Python 3.8

I am logging into a Django project in an app: SupplierPortal and am trying to link a Nav button to CashMgmnt.

Here is that href in base.py:

<li ><a href="{% url 'CashMgmt:customer-detail' pk=id %}">Cash Management</a></li>

Here is the urls.py:

urlpatterns = [
    path('customer/customerdetail/<int:pk>', views.AppCustomerCstDetailView.as_view(), name='customer-detail'),
]

Here is the views.py:

class AppCustomerCstDetailView(generic.DetailView):
    model = AppCustomerCst
    paginate_by = 10

    def get_absolute_url(self):
        return reverse('customer-detail', args=[str(self.id)])

When loading I'm getting the following error:

Reverse for 'customer-detail' with no arguments not found. 1 pattern(s) tried: ['customer/customerdetail/(?P<pk>[0-9]+)$']

Upvotes: 0

Views: 32

Answers (1)

Al Mahdi
Al Mahdi

Reputation: 820

you are taking a int argument in your urlpattern

urlpatterns = [
    path('customer/customerdetail/<int:pk>', views.AppCustomerCstDetailView.as_view(),name='customer-detail'),
]

but you are passing str argument in the reverse function

class AppCustomerCstDetailView(generic.DetailView):
    model = AppCustomerCst
    paginate_by = 10

    def get_absolute_url(self):
        return reverse('customer-detail', args=[str(self.id)])

the reverse function should be like this

reverse('customer-detail', args=[int(self.id)])

Upvotes: 1

Related Questions