为什么这个Django URL解析不起作用

时间:2016-09-05 16:32:33

标签: python django django-views

我在/urls.py文件中有以下Django url配置

from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib import admin
import core
import core.views as coreviews

urlpatterns = [
    #url(r'^admin/', admin.site.urls),
    #url(r'^create/$', coreviews.handleFile), #Uncommenting this works
    url(r'^create/$', include('core.urls')),  #This doesn't work
    url(r'^$', include('core.urls')),#Make msg app the the default one
    #url(r'^upload/', include('core.urls')),
]

我的core / urls.py文件定义如下

from django.conf.urls import include, url
from django.contrib import admin
import core.views as coreviews
from django.core.urlresolvers import reverse
from django.conf import settings
from django.contrib.auth.decorators import login_required

urlpatterns = [
    url(r'my$', coreviews.handleFile),
    url(r'^$', coreviews.index),
]

最后,core / views.py文件定义如下

def index(request):
    return render_to_response('AudioRecorder/index.html', context_instance=RequestContext(request))

def handleFile(request):
    return render_to_response('AudioRecorder/index.html', context_instance=RequestContext(request))

show_urls输出如下

/   core.views.index        
/create/    core.views.index        
/create/my  core.views.handleFile       
/my core.views.handleFile

问题: 我在localhost上运行。当我向网址http://localhost:8000/发送获取请求时,我会得到200个代码的正确回复。但是当我向网址http://localhost:8000/create/my发送get请求时,我发现404找不到错误。为什么会这样?难道第二个URL也不会返回200个代码吗?

1 个答案:

答案 0 :(得分:3)

因为你用$来终止包含url模式,这意味着模式的结束。没有什么能比得上。删除这些字符:

url(r'^create/', include('core.urls')),
url(r'', include('core.urls')),