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


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}"



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
          return reverse_lazy('company', kwargs={'pk': companyid})

This should work perfectly.


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.


Leave a comment