What is the procedure for evaluating urls.py regexes in django?

I had problems with regex in urls.py (I start with django as well as regexes)

Here is my original urls.py

url(r'^name/(?P<name>\w+)/$', 'course.views.name'), url(r'^', 'course.views.index'), 

And I tried to access it using this:

 http://127.0.0.1:8000/name/blah/ 

My view looks like this:

 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 that I got was that no matter what contribution I made, I would regularly get the "index" function, not the "name" function. I thought the problem was with the first regex.

But then I changed the second to:

 url(r'^$', 'course.views.index'), 

And IT works the way I thought it works!

I understand that "$" means the end of the line, but shouldn't the first regex be evaluated first? What is the order of matching of these expressions?

Adding "$" to each URL is not a big deal, but I would like to understand why I am posting it.

I am using Django1.4 and Python 2.7

+7
source share
2 answers

Read the Django Document

How Django Handles a Request

When a user requests a page from your Django-powered site, this is the algorithm that the system should determine which Python code to execute:

  • Django defines the URLconf root module used. This is usually the value of the ROOT_URLCONF parameter, but if the incoming HttpRequest object has the urlconf attribute (set by the request processing middleware), its value will be used instead of ROOT_URLCONF.
  • Django loads this Python module and looks for urlpatterns variables. This should be a Python list, in the format returned by the django.conf.urls.patterns () function.
  • Django starts each URL pattern in order and stops at the first one that matches the requested URL.
  • As soon as one of the regular expressions matches, Django imports and calls the given view, which is a simple Python function. The view is passed to HttpRequest as the first argument and any values ​​written in the regular expression as the rest of the arguments.
  • If no regular expression matches, or if an exception occurs at any point in the process, Django invokes the appropriate error handling representation. See Error Handling Section below.

He 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 mistake.

You must add $ to each url pattern if Including other URLconfs

+3
source

You are right, django doc reports:

How Django Handles a Request

When a user requests a page from your site running on Django, this is the algorithm that the system follows to determine the Python code to execute:

  • Django defines the URLconf root module used. This is usually the value of the ROOT_URLCONF parameter, but if the incoming HttpRequest object has the urlconf attribute (specified by the middleware request processing), its value will be used instead of the ROOT_URLCONF parameter.
  • Django loads this Python module and looks for urlpatterns variables. This should be a Python list, in the format returned by the django.conf.urls.patterns () function.
  • Django starts each URL pattern in order and stops at the first one that matches the requested URL.
  • As soon as one of the regular expressions matches, Django imports and calls the given view, which is a simple Python function. The view is passed to HttpRequest as the first argument and any values ​​written in the regular expression as the rest of the arguments.
  • If no regular expression matches or if an exception occurs at any point in the process, Django calls the appropriate error handling View. See Error Handling Section below.

It is also that another SO post suggests fixing yet another URL rating issue.

+1
source

All Articles