Support for old and new versions of URIs that work without breaking the reverse ()

How can I support old and new versions of URIs that work without violating the reverse () function?

For example, I have:

urlpatterns = patterns('',
    url(r'^(old_part|new_part)/other/$', 'some_view'),
)

In this case, /old_part/other/they /new_part/other/point to the same view, but the reverse () method fails because it does not know how to form the link correctly.

Also, what if we have url(r'^(old_part|new_part)/other/', include(sub_patterns)), how can it be processed?

Do you have any ideas?

Thank you for your help.

+4
source share
3 answers

, . , url, , URL-. , 301 HTTP ( ).

URL- SEO. Google .

Django:

from django.views.generic import RedirectView

urlpatterns = patterns('',
    url(r'^new_part/other/$', 'some_view'),
    url(r'^old_part/other/$', 
        RedirectView.as_view(url='new_part/other/', permanent=True)),
)

, url url :

urlpatterns = patterns('',
    url(r'^new_part/other/$', include(sub_patterns)),
    url(r'^old_part/other/(?P<rest>.*)$', 
        RedirectView.as_view(url='new_part/other/%(rest)s', permanent=True)),
)

redirect_to Django 1.4 .

+3

,

url(r'^(?P<url>old_part|new_part)/other/$', 'some_view', name='some_view'),

:

def some_view(request, url):
...

reverse :

# will return /old_part/other/
reverse('some_view', kwargs={'url': 'old_part'})

# will return /new_part/other/
reverse('some_view', kwargs={'url': 'new_part'})
+1

URL- ( 301 ).

NB: if you really insist on supporting both sets of URLs (not a good IMHO idea), you will have to have two different url patterns and choose what you need for reverse().

0
source

All Articles