[Django]-How to redirect to a dynamic url in django

3👍

Solution

The direct solution to your question would be the following:

from django.urls import reverse
from django.shortcuts import get_object_or_404
...
instance = get_object_or_404(Object, name=name, job=job)
redirect(reverse('test:output_page', args=instance))

However, it would be worth investigating class based views. I recommend using django’s built in RedirectView for this purpose.

References

Django Reverse: https://docs.djangoproject.com/en/3.1/ref/urlresolvers/

Django RedirectView: https://docs.djangoproject.com/en/3.1/ref/class-based-views/base/#redirectview

👤pygeek

3👍

You can do like this

return redirect(reverse("test:ouput_page",kwargs={'instance':str(instance_id)}))

-1👍

instance_model = "xyz"
return redirect('output_page', instance=str(instance_model))
(or)
return redirect('test:output_page', instance=str(instance_model))

Leave a comment