tags:

views:

37

answers:

3

Hi there,

Simple question.

How can I pass a variable from the URL to the view? I'm following the Date Example.

My view needs to arguments:

def hours_ahead(request, offset):

My url.py has this"

(r'^plus/\d{1,2}/$', hours_ahead),

I know I need to pass another argument through but I don't know how to get the number from the URL string (i.e. 'time/plus/4/'. Something like this?

(r'^plus/\d{1,2}/$', hours_ahead, offset=??),
+1  A: 

You could use Named groups in the regex

(r'^plus/(?P<offset>\d{1,2})/$', hours_ahead),
pycruft
A: 

I am not sure I understand your question clearly. Here is my shot at answering based on what I understood.

Adding a named regex group to your URL configuration should help:

(r'^plus/(?P<offset>\d{1,2})/$', hours_ahead),

This will let you keep the existing view:

def hours_ahead(request, offset):
    ...
Manoj Govindan
A: 

you can add additional variables like this:

(r'^plus/\d{1,2}/$', {'hours' : 5},  hours_ahead),

Best regards!

o.elias