Django redirect() with anchor (#) parameters

redirect() accepts URL, you could use reverse() to get one and appending hash part:

from django.core.urlresolvers import reverse

return redirect(reverse('main.views.home', kwargs={'home_slug':slug}) + '#first')
# or string formatting
return redirect('{}#first'.format(reverse('main.views.home', kwargs={'home_slug':slug})))

Also, there is a shortcut django.shortcuts.resolve_url which works like:

'{}#first'.format(resolve_url('main.views.home', home_slug=slug))

EDIT for Django 2.0, use:
from django.urls import reverse

Leave a Comment