【发布时间】:2016-01-01 16:48:14
【问题描述】:
我有以下 urls.py:
from django.conf import settings
from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views.generic.base import RedirectView
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', 'news_readr.views.home', name='home'),
url(r'^details/(?P<article_id>[0-9]+)$', 'news_readr.views.details', name='details'),
url(r'^details/$', 'news_readr.views.details', name='details'),
url(r'^/$', 'news_readr.views.home', name='home'),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我的应用中有两个有效的 URL:
- 本地主机:8000/
- localhost:8000/details/123 #其中123可以是任意数字
我想在其中放置一个正则表达式来处理其他情况并将这些请求路由回“主”视图。但我尝试的任何方法似乎都不起作用。我尝试将这些作为我的 urlpattern 的最后一行:
url(r'^/$', 'news_readr.views.home', name='home'), #this does nothing
url(r'', 'news_readr.views.home', name='home'), #this redirects fine to my homepage, but breaks all of my media and static paths, and causes my images to not load
我可以使用更好的方法或正确的正则表达式来解决这种情况吗?
【问题讨论】:
标签: python regex django django-urls url-pattern