I was having some problems with the regex in urls.py (I am a beginner to django as well as regexes in general)
Here is my original urls.py
url(r'^name/(?P<name>\w+)/$', 'course.views.name'),
url(r'^', 'course.views.index'),
And I was trying to access it using this:
http://127.0.0.1:8000/name/blah/
My view looks like:
def index(request):
return HttpResponse("Hello, sam. You're at the course index.")
def name(request, name):
return HttpResponse("Hello, %s. You're at the course index." % name)
The result I was getting was that for no matter what input I gave, I would regularly get the “index” function, and not the “name” function. I thought the problem was with the first regex.
But then, I changed the the 2nd one to:
url(r'^$', 'course.views.index'),
And THIS works just the way I figured it to work!
I understand that “$” means end of line, but shouldnt the 1st regex have been evaluated first? What is the order in which these expressions are matched?
Adding a “$” to every url is not that big a deal, but I would like to understand why I am putting it there.
I am using Django1.4 and Python 2.7
Read the Django document
It said
3. Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL.So I think this is a bug.You should add
$in every url pattern unlessIncluding other URLconfs