我想在我的 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 集。