user763410 user763410 - 2 months ago 35
Python Question

Why is this Django URL resolution not working

I have the following Django url configuration in /urls.py file

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


My core/urls.py file is defined as follows

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


Finally, core/views.py file is defined as follows

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 output is as follows

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


Question:
I am running on localhost. When I send get request to url http://localhost:8000/, I get proper response with 200 code. But when I send get request to url http://localhost:8000/create/my, I get 404 not found error. Why is it happening like this? Shouldn't second URL also return 200 code?

Answer

Because you're terminating the including url pattern with a $, which means the end of the pattern. Nothing can match past that. Remove those characters:

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