tags:

views:

34

answers:

2

I have a route defined as follows:

(r'^edit/(\d+)/$', 'app.path.edit')

I want to use the reverse function as follows:

url = reverse('app.path.edit', args=('-id-',))

The generated url gets passed to a js function, and client side code will eventually replace '-id-' with the correct numeric id. This of course won't work, because the 'reverse' function won't match the route, because the url is defined as containing a numeric argument.

I can change the route to accept any type of argument as follows, but then I loose some specificity:

(r'^edit/(.+)/$', 'app.path.edit'

I could create a separate url for each item being displayed, but I'll be displaying many items in a list, so it seems like a waste of bandwidth to include the full url for each item.

Is there a better strategy to accomplish what I want to do?

+1  A: 

You can rewrite regexp like this:

(r'^edit/(\d+|-id-)/$', 'app.path.edit')

but I generally prefer this:

(r'^edit/([^/]+)/$', 'app.path.edit') # you can still differ "edit/11/" and "edit/11/param/"

Usually you will anyway need to check entity for existent with get_object_or_404 shortcut or similar, so the only bad is that you have to be more accurate with incoming data as id can contain almost any characters.

Vladimir
+1  A: 

In my opinion, and easier solution would be to keep the original url and then pass the value '0' instead of '-id-'. In the client side then you replace '/0/' with the correct id. I think this is better because it doesn't obscure the url routing, and you don't lose specificity.

Tiago Brandes
But /0/ could also be a valid id, so it's not clear if /0/ is a place holder or an id.
limscoder
Yeah, in my case it's ok to use it, because the ID is the database's auto-increment primary key.
Tiago Brandes