0

我想在我的 django 应用程序中有虚荣网址,即用户的个人资料在 url 之类的example.com/username. 我试着像显示的那样做:

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'),
   )

由于虚 url 的 url 模式最终在 urlpatterns 中,所以 django 应该最后匹配它。但是,即使它与管理员 url 和 user_profile 视图冲突,它也会呈现 'example.com/admin' url 而不是默认值。确保 vanity-url 不与任何 django-url 冲突的最佳方法是什么?无论如何要编写正则表达式,以便它排除 django 应用程序的现有 url 集。

4

1 回答 1

1

您的中间件重定向/admin//admin. 这与管理员的正则表达式不匹配,仅适用于您的用户配置文件。使用此中间件,您无法访问管理索引页面。

一种解决方案是仅在带有斜杠的旧路径无效且新路径有效时才重定向。

于 2015-08-01T10:27:25.240 回答