在Django的Javascript干网址 [英] DRY URLs in Django Javascript

查看:188
本文介绍了在Django的Javascript干网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Django的AppEngine上。我使用Django的反向()功能无处不在,保持原有的一切尽可能的干燥。

不过,我在此应用到我的客户端JavaScript的麻烦。有加载一些数据取决于传入的ID的JS类。有没有一个标准的办法不硬code中的URL,这个数据应该来自哪里?

  VAR RQ =新Request.HTML({
    更新:this.element,
})得到('/模板/+ template_id +'/ preVIEW')。 //困扰我的一部分。
 

解决方案

最合理的解决方案似乎是通过URL列表中的JavaScript文件,并具有相当的JavaScript的反向的()可在客户端上。唯一的反对意见可能是整个URL结构露出。

下面是这样一个功能(此的question )。

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 applying this to my client-side javascript. There is a JS class that loads some data depending on a passed-in ID. Is there a standard way to not-hardcode the URL that this data should come from?

var rq = new Request.HTML({
    'update':this.element,
}).get('/template/'+template_id+'/preview'); //The part that bothers me.

解决方案

The most reasonable solution seems to be passing a list of URLs in a JavaScript file, and having a JavaScript equivalent of reverse() available on the client. The only objection might be that the entire URL structure is exposed.

Here is such a function (from this question).

这篇关于在Django的Javascript干网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆