Best-practice for building URLs in templates with JavaScript

46 views
Skip to first unread message

Shawn Milochik

unread,
Apr 22, 2011, 9:41:10 AM4/22/11
to django...@googlegroups.com
I'm working on an app which makes extensive use of AJAX. I'm also
using named URLs so I don't have to hard-code URLs into my templates.

The problem is that if a URL pattern requires extra info after the
path, my templates don't render, because the extra parameters aren't
known until the user makes a selection.

The obvious solution is to make two URL patterns for each of these
URLs -- one with no extra parameters and one with. Then change the
former into the latter in the app by AJAX.

Is there a better way to do this?

Example:

#urls.py:
url(r'^update_payment/(?P<payment_id>\w+)', update_payment, name =
'update_payment'),

#template:
<a href="{% url update_payment %}">Update Payment</a> {# this
expects a second argument I don't know yet #}

Thanks,
Shawn

Ryan Osborn

unread,
Apr 23, 2011, 3:31:19 AM4/23/11
to django...@googlegroups.com
You could always make the payment_id group optional using a ?:

update_payment/(?P<payment_id>\w+)?

that way this will match either:

update_payment/

or

update_payment/123

Hope that helps,

Ryan

Artur Wdowiarski

unread,
Apr 23, 2011, 4:19:03 AM4/23/11
to django...@googlegroups.com, django...@googlegroups.com
If it's quite a big project with lots of ajax, I'd say it's worth a try to serve your urls to the client in some reasonable way and then write an equivalent of reverse() in javascript.


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

Shawn Milochik

unread,
Apr 23, 2011, 11:43:13 AM4/23/11
to django...@googlegroups.com
On Sat, Apr 23, 2011 at 3:31 AM, Ryan Osborn <ryan.o...@gmail.com> wrote:
> You could always make the payment_id group optional using a ?:
>
> update_payment/(?P<payment_id>\w+)?


Ryan,

Thanks. I considered that first, but rejected it because I want the
extra field to be required. If it's missing then it's an error. Giving
it a default in the view definition means extra code in all effected
views, which isn't DRY. Also a bug caused by missing parameters that
cause an error is more subtle than the (admittedly ugly) "duplicate"
url pattern.

I was hoping that, since the url tag, named URLs, and URLs with extra
parameters are all baked-in parts of Django that Django would have a
canonical way to deal with this, since URLs built based on user
actions are a very old technique. It seems like someone must have run
into this before me.

It occurs to me now that I could just use a placeholder, and replace
that in JavaScript:
<a href="{% url update_payment 'payment_id' %}">Update Payment</a>

Then use the JavaScript to replace the string 'payment_id' (or a regex
on its location in the URL) when the user takes action. It also moves
all of the logic out of views and urls, which I think is probably
best.

Shawn

akaariai

unread,
Apr 23, 2011, 2:40:35 PM4/23/11
to Django users
On Apr 23, 6:43 pm, Shawn Milochik <sh...@milochik.com> wrote:
> It occurs to me now that I could just use a placeholder, and replace
> that in JavaScript:
>     <a href="{% url update_payment 'payment_id' %}">Update Payment</a>
>
> Then use the JavaScript to replace the string 'payment_id' (or a regex
> on its location in the URL) when the user takes action. It also moves
> all of the logic out of views and urls, which I think is probably
> best.

This is exactly what I have been doing. It is bit of a hack, but works
very well in practice.

- Anssi
Reply all
Reply to author
Forward
0 new messages