Monday, October 22, 2012

Re: urls.py and default values...

On Mon, Oct 22, 2012 at 5:39 PM, Tomas Ehrlich <tomas.ehrlich@gmail.com> wrote:
> Hello,
> you need to make 'year' argument optional:
>
> def student_reports(request, year=None):
> year = year or datetime.date.today().year
> ...

Perfect - thanks



>
> Another option could be passing extra option to you view function:
> https://docs.djangoproject.com/en/1.4/topics/http/urls/#passing-extra-options-to-view-functions
>
>
> Cheers,
> Tom
>
> Dne Mon, 22 Oct 2012 15:24:51 +1200
> Lachlan Musicman <datakid@gmail.com> napsal(a):
>
>> Hola,
>>
>> I have data across multiple years.
>>
>> I want to run reports on a per year basis, so I have in my urls:
>> url(r'^students/reports/(?P<year>\d{4})/$', student_reports,
>> name='student_reports'),
>>
>> etc.
>>
>> What I want to know though, is how to have this in the urls:
>> url(r'^students/reports/$', student_reports, name='student_reports'),
>>
>> defaulting to the year it's run. (ie, year = datetime.date.today().year)
>>
>> I thought about passing extra options, but it feels like overkill?
>>
>> I have in my views.py:
>>
>> def student_reports(request, year):
>> if not year:
>> year = datetime.date.today().year
>> yada yada
>>
>>
>> but this is failing on not enough arguments...
>>
>>
>> Cheers
>> L.
>>
>>
> z`
>
>
> S pozdravem
> Tomáš Ehrlich
>
> Email: tomas.ehrlich@gmail.com
> Tel: +420 608 219 889
> Jabber: elvard@jabber.cz
>
> "Půjdu kamkoliv, pokud je to kupředu." - J. London
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django-users@googlegroups.com.
> To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>



--
...we look at the present day through a rear-view mirror. This is
something Marshall McLuhan said back in the Sixties, when the world
was in the grip of authentic-seeming future narratives. He said, "We
look at the present through a rear-view mirror. We march backwards
into the future."

http://www.warrenellis.com/?p=14314

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment