Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7049715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:03:19+00:00 2026-05-28T03:03:19+00:00

I am trying to follow what is being explained here: http://www.djangobook.com/en/2.0/chapter03/ (the confusing example

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:03:20+00:00Added an answer on May 28, 2026 at 3:03 am

    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 re module and an asterisk.

    import re
    
    def myfunc1arg(arg1):
        print "I got passed the argument", arg1
    
    def myfunc2args(arg1, arg2):
        print "I got passed the arguments", arg1, "and", arg2
    
    myfunc1arg(*re.match("args/(.*)", "args/hello").groups())
    
    myfunc2args(*re.match("args/(.*)/(.*)", "args/hello/world").groups())
    

    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:

    def resolve(self, path):
        match = self.regex.search(path)
        if match:
            # If there are any named groups, use those as kwargs, ignoring
            # non-named groups. Otherwise, pass all non-named arguments as
            # positional arguments.
            kwargs = match.groupdict()
            if kwargs:
                args = ()
            else:
                args = match.groups()
            # In both cases, pass any extra_kwargs as **kwargs.
            kwargs.update(self.default_args)
    
            return ResolverMatch(self.callback, args, kwargs, self.name)
    

    What it has there in the ResolverMatch are args, a list of positional arguments, and kwargs, a list of keyword arguments (which can be created via named capturing groups).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to follow a simple example of AJAX-enabled WCF Service like: http://www.pluralsight.com/community/blogs/fritz/archive/2008/01/31/50121.aspx I'm
I'm trying to follow the MVVM pattern laid out here: http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090097 I have this
I am trying to follow the steps on the following site http://msdn.microsoft.com/en-us/library/ms181089(VS.80).aspx But I
I've been trying to follow this Railscast: http://railscasts.com/episodes/213-calendars I've implemented the controls and all
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
I am trying to follow this example (from p137 of Rob Pickering's Foundations of
I am trying to follow this article: http://robshouse.net/article/xdebug-komodo-and-acquia-drupal-stack-installer I did all of that, yet
I'm trying to follow a tutorial here: regarding overloading operators, and I've found something
So, i'm trying to follow along with this example . I'm trying to translate
I am trying to accomplish much the same thing as is being done here

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.