[Solved]-How to call reverse() with an argument for a given url that has an argument?

19👍

Give urls names:

from django.urls import reverse

urlpatterns = patterns('',
    url(r'^$',JobList.as_view(), name='joblist'),
    url(r'^(?P<pk>\d+)/$', JobDetail.as_view(), name='jobdetail'),
)

Use that name when call reverse:

return reverse('jobdetail', args=[self.pk])

or

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

Leave a comment