I want to have vanity urls in my django application i.e. user's profile at url like example.com/username
. I tried to do it like shown:
urlpatterns = patterns('',
#sitemap generation
url(r'^sitemap\.xml$', sitemap, {'sitemaps': sitemaps},name='django.contrib.sitemaps.views.sitemap'),
url(r'^grappelli/', include('grappelli.urls')), # grappelli URLS
url(r'^admin/', include(admin.site.urls)),
..other urls
#User Profile URLs
url(r'^(?i)(?P<username>[a-zA-Z0-9.-_]+)$','myapp.views.user_profile',name='user-profile'),
)
As url pattern for vanity urls is at last in urlpatterns so django should match it at the last. But even though its conflicting with the admin urls and user_profile view renders 'example.com/admin' url instead of default.What's the best way of ensuring that vanity-urls do not clash with any of the django-urls ? Is there anyway to write regex such that it excludes the set of existing urls of the django application.
Your middleware redirects /admin/
to /admin
. This does not match the regex for the admin, only for your user profile. With this middleware you cannot reach the admin index page.
One solution is to only redirect if the old path, with slashes, is invalid and the new one is valid.