View function and URL Conf basic question

29 views
Skip to first unread message

ApathyBear

unread,
Feb 26, 2014, 3:10:40 AM2/26/14
to django...@googlegroups.com
I am brand new to Django, so bare with me.

I am a little confused with this example shown in the Django book:

Here is my urls.py:

from django.conf.urls.defaults import *
from mysite.views import hello, current_datetime, hours_ahead

urlpatterns = patterns('',
    url(r'^hello/$', hello),
    url(r'^time/$', current_datetime),
    url(r'^time/plus/(\d{1,2})/$', hours_ahead),
)

And here is my View function associated with hours_ahead
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)


Now what is throwing me off is how 'offset' is a second argument to  the hours_ahead function. Yet I am not quite sure how it being the second argument makes it the case that it is associated with whatever is entered as a URL. Let me use an example to illustrate my confusion.

Say I request the URL:  http://127.0.0.1:8000/time/plus/2/ . Why is it the case that offset is '2'? I am not seeing why '2' is plucked out? What happened with 'time' and 'plus'? How does python/django know that the '2' is referring to offset?

Thanks for any help in advance.



Daniel Roseman

unread,
Feb 26, 2014, 8:54:51 AM2/26/14
to django...@googlegroups.com
That is what the regular expression does in your first snippet. `time` and `plus` are just matched, but not captured: the only thing that is captured is `(\d{1,2})`, because it is surrounded in parentheses.

If that's not clear for you, you should read a guide to regexes: http://regular-expressions.info  is a good one.
--
DR.

Camilo Torres

unread,
Feb 27, 2014, 8:17:04 PM2/27/14
to django...@googlegroups.com
On Wednesday, February 26, 2014 9:24:51 AM UTC-4:30, Daniel Roseman wrote:
On Wednesday, 26 February 2014 08:10:40 UTC, ApathyBear wrote:
Here is my urls.py:
from django.conf.urls.defaults import *
from mysite.views import hello, current_datetime, hours_ahead

urlpatterns = patterns('',
    url(r'^hello/$', hello),
    url(r'^time/$', current_datetime),
    url(r'^time/plus/(\d{1,2})/$', hours_ahead),
)
And here is my View function associated with hours_ahead
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)
Now what is throwing me off is how 'offset' is a second argument to  the hours_ahead function. Yet I am not quite sure how it being the second argument makes it the case that it is associated with whatever is entered as a URL. Let me use an example to illustrate my confusion.

Say I request the URL:  http://127.0.0.1:8000/time/plus/2/ . Why is it the case that offset is '2'? I am not seeing why '2' is plucked out? What happened with 'time' and 'plus'? How does python/django know that the '2' is referring to offset?
That is what the regular expression does in your first snippet. `time` and `plus` are just matched, but not captured: the only thing that is captured is `(\d{1,2})`, because it is surrounded in parentheses.

If that's not clear for you, you should read a guide to regexes: http://regular-expressions.info  is a good one.
Hello,

As Daniel points out, this is related to regular expressions. You can read a full explanation here:

You sholud also take a look at the 're' Python module. 

Regards,
Camilo.
Reply all
Reply to author
Forward
0 new messages