简体   繁体   English

虚荣网址与管理员网址冲突

[英]vanity urls clashing with admin urls django

I want to have vanity urls in my django application ie user's profile at url like example.com/username . 我想在我的django应用程序中使用虚荣网址,即用户的个人资料网址,例如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. 由于虚荣网址的网址格式最后是urlpatterns中的,因此django应该最后匹配它。 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 ? 但是,即使它与admin网址和user_profile视图冲突,也会呈现出example.com/admin网址而不是默认网址。确保虚荣网址不与任何django网址冲突的最佳方法是什么? Is there anyway to write regex such that it excludes the set of existing urls of the django application. 无论如何,有没有编写正则表达式,使其排除了django应用程序的现有URL集。

Your middleware redirects /admin/ to /admin . 您的中间件将/admin/重定向到/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. 一种解决方案是仅在带有斜杠的旧路径无效而新路径有效的情况下重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM