On Sep 23, 5:24 pm, IanSR <ijsto...@crystal.harvard.edu> wrote:
> Is there some trick to parse
>
> http://www.example.com/foo/42
>
> such that "42" becomes an integer parameter to the view function,
> instead of a string?
>
> Right now I have to do:
>
> from my.app.views import foo
>
> urlpatterns = patterns('',
>     (r'foo/(?P<bar_str>\d+)/?$'    , foo),
> )
>
> my/app/views.py
>
> def foo(request, bar_str):
>     bar = int(bar_str)
>
> This is obviously not so convenient.  It would be much nicer to simply
> have the pattern auto-converted to an integer.  I understand the
> Python reasons for why it works this way, but if there were some
> Django shortcut to help with this, that would be great.
>
> Ian

There's no built-in way. If you really need this, the best way might
be to write a decorator that converts the arguments and passes them on
to the wrapped function. I doubt it'll end up much less verbose than
just calling int(), though.
--
DR.
--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to