[Answer]-Django: Permanent redirect of URL with regex parameters

1👍

It seems I’ve found my answer in the django docs – I didn’t look hard enought after all!

https://docs.djangoproject.com/en/1.1/ref/generic-views/

urlpatterns = patterns('django.views.generic.simple',
     ('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}),
)

0👍

First of all you need to do some changes in the url. Use url function and then give a name to the url. You have some issues in your url, for example you have used ?P but did’nt give a name to the capturing group. Second [\d]*? there is no need for ? because * means there can be a digit or not at all. So after considering all the above mentioned bugs and techniques in the end your url should look like this:

url(r'^liste-over-andelsboligforeninger/(?P<cooperative_id>\d*)/$', 'cooperatives', name="cooperatives")

Then in the view you can use reverse url resolution as:

redirect(reverse('cooperatives', kwargs={'cooperative_id': some_id}))

Leave a comment