I am trying to follow what is being explained here: http://www.djangobook.com/en/2.0/chapter03/ (the confusing example is about 4/5 of the way down the page).
The idea is to have a URL in which any one of /time/plus/1, /time/plus/2, /time/plus/3 etc — all the way up to /time/plus/99 — could be matched. The book says:
How, then do we design our application to handle arbitrary hour
offsets? The key is to use wildcard URLpatterns. As we mentioned
previously, a URLpattern is a regular expression; hence, we can use
the regular expression pattern \d+ to match one or more digits:
Since we want to stop at 99, the book suggests using the following:
urlpatterns = patterns('',
# ...
(r'^time/plus/\d{1,2}/$', hours_ahead),
# ...
)
But now we are faced with the problem of capturing exactly which number the regular expression matches and using it in our calculations. The book’s explanation proceeds:
Now that we’ve designated a wildcard for the URL, we need a way of
passing that wildcard data to the view function, so that we can use a
single view function for any arbitrary hour offset. We do this by
placing parentheses around the data in the URLpattern that we want to
save. In the case of our example, we want to save whatever number was
entered in the URL, so let’s put parentheses around the\d{1,2}, like
this:
(r'^time/plus/(\d{1,2})/$', hours_ahead),
If you’re familiar with regular expressions, you’ll be right at home
here; we’re using parentheses to capture data from the matched text.
Okay, I understand that the data is being captured — but where is it being stored? How does Django know that it needs to pass the captured data to the hours_ahead function? Indeed, one commentator on the books website even asks the following question:
It’s not clear from the description how saving the number entered in
the URL – by putting parentheses around d{1,2} – allows this value to
be passed as a parameter to hours_ahead.
Can you explain how the captured data from the URL get’s passed to the hours_ahead function?
In case you’re interested, here is the function from the views.py file:
from django.http import Http404, HttpResponse
import datetime
def hours_ahead(request, offset):
try:
offset = int(offset)
except ValueError:
raise Http404()
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
return HttpResponse(html)
The parentheses form what is called a regular expression capturing group, as you allude to in your question.
I don’t have the Django code in front of me, but you could actually do something like this yourself using the
remodule and an asterisk.The first argument to re.match is the regular expression (the pattern you are putting in your code) and the second argument is the “url”. Django uses a line of code like the above to pull out the groups in parentheses (the capturing groups) and pass them to your function.
ETA: If you’re interested, here (lines 195-209) is the specific Django code that captures the regular expressions from the URL path:
What it has there in the ResolverMatch are
args, a list of positional arguments, andkwargs, a list of keyword arguments (which can be created via named capturing groups).