Calling Django `reverse` in client-side Javascript

前端 未结 10 525
无人共我
无人共我 2020-12-13 02:22

I\'m using Django on Appengine. I\'m using the django reverse() function everywhere, keeping everything as DRY as possible.

However, I\'m having trouble

10条回答
  •  孤街浪徒
    2020-12-13 02:50

    Having just struggled with this, I came up with a slightly different solution.

    In my case, I wanted an external JS script to invoke an AJAX call on a button click (after doing some other processing).

    In the HTML, I used an HTML-5 custom attribute thus

    Then, in the javascript, simply did

    $.post($("#test-button").attr("data-ajax-target"), ... );
    

    Which meant Django's template system did all the reverse() logic for me.

提交回复
热议问题