Django reverse() for JavaScript

前端 未结 9 1077
情深已故
情深已故 2020-12-24 07:24

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

9条回答
  •  再見小時候
    2020-12-24 07:46

    What I usually do is put the URL in either an element, or in the rel="" attribute.

    Then, when writing the JS (using jQuery below) I do:

    $('div#show_more').click(function () {
        var url = $(this).attr('rel');
        // or
        var url = $('#more_url').val();
    
        $.get(url, function () { /* ... */ });
    });
    

    Nonstandard attributes are well supported by all major browsers and hidden elements don't have to be in forms.

提交回复
热议问题