37👍
✅
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
👤okm
9👍
[Only working up until Django 1.8, not functional in Django 1.9+, see comments!]
You can add an anchor into the regular expression in urls.py. Here is an example from a sample forum app which will jump to the desired post in a thread.
views.py
return redirect(post_list,
slug=post.thread.slug,
page=1,
anchor='post_{0}'.format(post.id)
)
urls.py
url(r'^thread/(?P<slug>[-\w]+)/(?P<page>[0-9]+)/#(?P<anchor>[-_\w]+)$', post_list, name='forum_view_thread'),
url(r'^thread/(?P<slug>[-\w]+)/(?P<page>[0-9]+)/$', post_list, name='forum_view_thread'),
url(r'^thread/(?P<slug>[-\w]+)/$', post_list, name='forum_view_thread'),
👤scum
0👍
just use:
url(r'^app/#/welcome/$', SomeView.as_view(), name='welcome'),
from django.utils.encoding import uri_to_iri
uri_to_iri(urlresolvers.reverse('welcome'))
Source:stackexchange.com