Django reverse()for JavaScript [英] Django reverse() for JavaScript
问题描述
在我的项目中,我有很多 Ajax 方法,与外部客户端边脚本(我不想将JavaScript插入到模板中),而改变URL对我来说是一种痛苦,因为我需要手动更改Ajax调用中的URL。
有没有办法在JavaScript中模拟 {%url%}
templatetag的行为?
例如,以 ^ ajax
开头的打印urlpattern,然后在脚本中替换其实际值的模式?
这是我的想法,我的问题是 - 有什么常见的做法做这样的事情吗?也许有一些可重用的应用程序?另外我也很乐意阅读您的任何建议和相关想法。
更新1:
我正在谈论计算URL,而不是静态URL:
url(r'^ ajax / delete /(?P< type> image | audio)/ (?P< item_id> \d +)/ from / set /(?P< set_id> \d +)/ $','blog.ajax.remove_item_from_set'),
/ pre>
解决方案https://github.com/mlouro/django-js-utils
dutils是一个小实用程序库旨在为JavaScript / Django开发人员提供一些可帮助在Django后端开发RIA的实用程序。
它目前支持以下功能:
- 生成Django网址的反向方法...
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'),
解决方案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...
这篇关于Django reverse()for JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!