Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django reverse() for JavaScript

In my project I have a lot of Ajax methods, with external client-side scripts (I don't want to include JavaScript into templates!) and changing URLs is kind of pain for me because I need to change URLs in my Ajax calls manually.

Is there is some way to emulate the behavior of {% url %} templatetag in JavaScript?

For example, print urlpatterns starting with ^ajax and later in scripts replace patterns with their actual values?

That's what on my mind, and my question is - are there any common practices to do things like that? Maybe some reusable applications? Also I will be happy to read any advices and relevant thoughts you have.

Update 1: I'm talking about computed URLs, not static ones:

url(r'^ajax/delete/(?P<type>image|audio)/(?P<item_id>\d+)/from/set/(?P<set_id>\d+)/$', 'blog.ajax.remove_item_from_set'),
like image 636
dir01 Avatar asked Nov 25 '09 09:11

dir01


5 Answers

Try creating javascript helper functions (in django template) for generating url string. In simple form they could look like this:

function generete_some_url(id){
    return "{% url some_url itemid=112233 %}".replace("112233", id);
}

Maybe this has some other implications but I think it should work.

like image 81
kodmasin Avatar answered Nov 18 '22 10:11

kodmasin


What's wrong with putting JavaScript in your templates?

You often want to call an initialisation function in your HTML template anyway, so why not pass it an object containing URLs you'll be using?

<script>
MYGLOBAL.mymodule.init({
    fancy_ajax_url: '{% url fancy %}',
    fancier_ajax_url: '{% url fancier %}'
});
</script>

If you find yourself passing a lot of variable this way, or wanting to use logic in your JavaScript that you do in your HTML templates, then why not render your script through Django's templating engine? Remember, Django templates are not just for HTML documents - often it helps to use templates for plain text, XML, JSON, and yes even JavaScript. Worried about performance? Then cache the result.

like image 34
ozan Avatar answered Nov 18 '22 10:11

ozan


I created a mechanism that builds a list of url patterns in your Django project and outputs that in a Javascript file. It is a fork of django-js-utils.

The repo link is here: https://github.com/Dimitri-Gnidash/django-js-utils

like image 12
Dimitri Gnidash Avatar answered Nov 18 '22 10:11

Dimitri Gnidash


https://github.com/mlouro/django-js-utils

dutils is a small utility library that aims to provide JavaScript/Django developers with a few utilities that will help the development of RIA on top of a Django Backend.

It currently supports the following features:

  • Reverse method for generating Django urls...
like image 10
dir01 Avatar answered Nov 18 '22 11:11

dir01


We created a small app called django-js-reverse for this purpose.

For example you can retrieve a named url

urls.py:

url(r'^/betterliving/(?P[-\w]+)/(?P\d+)/$', 'get_house', name='betterliving_get_house'),

in javascript like:

Urls.betterliving_get_house('house', 12)

result:

/betterliving/house/12/

like image 7
user1130588 Avatar answered Nov 18 '22 09:11

user1130588