如何放置"任何网址"在urlpattern列表的末尾

时间:2014-12-09 00:18:30

标签: django django-urls

我想将url(r'^', include('article.urls')),放在urlpatterns列表的末尾,因为在urlpatterns之后我有MEDIA_ROOT的网址:

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT}))

所以我需要url(r'^', include('article.urls'))成为最后一个。 我试过了

urlpatterns += patterns('', (r'^'), include('article.urls'),
)

但它不起作用。

urls.py

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'firstapp.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^redactor/', include('redactor.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^basicview/', include('article.urls')),
    url(r'^auth/', include('loginsys.urls')),
    url(r'^', include('article.urls')),

)

if settings.DEBUG:
    urlpatterns += patterns('',
        (r'^media/(?P<path>.*)$', 'django.views.static.serve', {
        'document_root': settings.MEDIA_ROOT}))

文章urls.py

urlpatterns = patterns('',
                       # Examples:
                       # url(r'^$', 'firstapp.views.home', name='home'),
                       # url(r'^blog/', include('blog.urls')),
                       url(r'^articles/$', 'article.views.articles'),
                       url(r'^articles/(?P<article_id>\d+)/$', 'article.views.article'),
                       url(r'^articles/addlike/(?P<article_id>\d+)/$', 'article.views.addlike'),
                       url(r'^articles/addcomment/(?P<article_id>\d+)/$', 'article.views.addcomment'),
                       url(r'^page/(\d+)/$', 'article.views.articles'),
                       url(r'^', 'article.views.mainPage'),
)

1 个答案:

答案 0 :(得分:0)

您可能需要urls.py

url(r'^.*', include('article.urls')),

在你的article.urls.py中你需要这个:

url(r'^.*', 'article.views.mainPage'),

您可以查看此question & answer