在客户端 Javascript 中调用 Django `reverse` [英] Calling Django `reverse` in client-side Javascript

查看:28
本文介绍了在客户端 Javascript 中调用 Django `reverse`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Appengine 上使用 Django.我在任何地方都使用 django reverse() 函数,尽可能保持一切干燥.

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

但是,我无法将其应用于我的客户端 javascript.有一个 JS 类根据传入的 ID 加载一些数据.是否有一种标准方法可以不对这些数据应该来自的 URL 进行硬编码?

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.

推荐答案

最合理的解决方案似乎是在 JavaScript 文件中传递 URL 列表,并在客户端提供与 reverse() 等效的 JavaScript.唯一的反对意见可能是暴露了整个 URL 结构.

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).

这篇关于在客户端 Javascript 中调用 Django `reverse`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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