On Mon, 2008-02-25 at 00:07 -0800, webheld wrote: > hi django guys, > > is there a simple way to convert URL parameter for views > automatically? > > let's say I have in my urls.py: > > (r'^myview/(\d+)/', 'myview') > > and I know that myview will *always* receive an int parameter, can > django convert this type somehow automatically?
No. We rely on Python's reg-exp library to match these strings. And the reg-exp library's position is that a string matching \d+ is still a string (quite reasonable). Doing automatic conversion would require inspecting the reg-exp string and working out which arguments *could* be converted, then which arguments *should* be converted (see below) and then doing the work. Lots of overhead. Note that automatic conversion wouldn't be a good plan, either. For example, the next version of my blog converts URLs like 2008/2/25/ to 2008/02/25/ because I want a canonical form. So I need to know if \d{1,2} matches one of two digits, even if the first one is 0. Auto-conversion to an integer would remove that capability (and it's not hard to think of other cases like this). So you'd need configurabiliy. One solution to your problem could be to write a decorator that takes a list of types (plus something like None for "don't care") and automatically converts argument N to the type in the N-th element of the list before calling your function. Regards, Malcolm -- The sooner you fall behind, the more time you'll have to catch up. http://www.pointy-stick.com/blog/ --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---