[Fixed]-How to reverse_lazy to a view/url with variable?

3👍

As Nickie suggested in the comments: https://docs.djangoproject.com/en/1.11/ref/class-based-views/mixins-editing/#django.views.generic.edit.DeletionMixin.success_url

Solved this with success_url = "/farm/{farm_id}"

👤Jon

28👍

Yes, you can do this with success_url in this way:

class DeleteAssignment(DeleteView):
    . . . . . 
    . . . . . 

    . . . . . 
    def get_success_url(self):
          # if you are passing 'pk' from 'urls' to 'DeleteView' for company
          # capture that 'pk' as companyid and pass it to 'reverse_lazy()' function
          companyid=self.kwargs['pk']
          return reverse_lazy('company', kwargs={'pk': companyid})

This should work perfectly.

0👍

The following works:

reverse_lazy('name_of_your_view', kwargs={'key': value})

Not documented either in the man page or in docstring, was suggested by AI.

👤Sumit

Leave a comment