Niladry Kar
Niladry Kar

Reputation: 1203

How to get the pk parameter in return redirect

I have a view:

@login_required
def add_auditor(request, pk, pk2):
    company_details = get_object_or_404(Company, pk=pk)
    user_profile = get_object_or_404(Profile, pk=pk2)

    company_details.auditor.add(user_profile.name)
    company_details.save()

    return redirect(reverse('company:search_auditors' , pk=company_details.pk))

The redirect url in this view is not working as it has a primary key parameter in it.

When I try to add_auditor or run the view it is throughing me this error:

TypeError: reverse() got an unexpected keyword argument 'pk'

How to pass a primary key parameter in redirect url?

Any idea

Upvotes: 0

Views: 299

Answers (2)

dxillar
dxillar

Reputation: 317

You should pass the kwargs in reverse() function by following syntax:

return redirect(reverse('company:search_auditors' , kwargs={'pk':company_details.pk}))

more details on reverse

Upvotes: 1

JPG
JPG

Reputation: 88619

The data should be passed through the kwargs argument as below,

return redirect(reverse('company:search_auditors', kwargs={"pk": company_details.pk}))

Ref : Django reverse()

Upvotes: 2

Related Questions