How to append string in the end of Django redirect in view?

I have a product on my platform and when a specific user does an action, I want to redirect them to example.com/product/product-slug/#SectionInWebsite.

However, I am not able to find a way to append "#SectionInWebsite" to the end of the redirect function.

return redirect('ProductSingle', product.slug, "#SectionInWebsite")

Maybe this can help you, using reverse.

return redirect(reverse('ProductSingle', product.slug, + '#SectionInWebsite')

do this :

return redirect(reverse('ProductSingle',product.slug) + '#SectionInWebsite')

This worked for me (I had to use different names on my machine, but it should work):

# views.py
return redirect('{}#sectionInWebsite'.format(reverse('ProductSingle', kwargs={'product_slug':product.slug})))

That is, assuming your urls.py has something like this:

# urls.py
...
path('ProductSingle/<str:product_slug>', views.ProductSingle, name='ProductSingle'),
...

This is just a variation of the answer provided here, applied to your situation.

Back to Top