从 JavaScript 调用 Django URL
*为了清晰起见编辑了这个问题*
我有一个 Django 模板,其中包含一个 flash 对象。模板本身继承自主模板,位于名为 info 的块中。一切正常!
单击 Flash 对象时,它会在单独的 js 文件中调用带有参数的 JavaScript 回调函数。在此函数中,我可以使用类似的内容加载另一个 Django 模板
function myCallBack(param){
location.href = 'crc_region_cities' // (Django url).
}
我想知道的是如何将参数 param (它是一个字符串)传递到该 URL,以便同名的视图函数: def crc_region_cities 选择它并执行适当的查询。
额外信息:
新的 URL 调用呈现模板的视图,该模板再次继承与先前模板相同的主模板,并驻留在同一块...信息中。
有没有更好的方法来做到这一点......?请记住,我的 Flash 对象使用字符串参数调用 JavaScript 回调函数,因此我必须以某种方式从那里操纵到新模板,同时将字符串参数传递给 URL。在下面的代码中您可以看到视图函数的第二个参数称为区域。这是我需要通过javascript函数传入的参数。
THE URL (r'^crc_region_cities/?$', 'crc_region_cities'),
The View
def crc_region_cities(request, region):
cities = City.objects.filter(region__exact=region)
return render_to_response('crc/crc_region_cities.html', {'cities': cities}, context_instance=RequestContext(request))
非常感谢
*EDITED THIS QUESTION FOR CLARITY*
I have a Django template with a flash object in it. The template itself inherits from the main template and is in a block called info. Everything works ok with that!
When the flash object is clicked it calls a JavaScript callback function, with a parameter, in a separate js file. Within this function I can load another Django template by using something like
function myCallBack(param){
location.href = 'crc_region_cities' // (Django url).
}
What I want to know is how do I pass the parameter param (which is a string) to that URL so that the view function of the same name: def crc_region_cities picks it up and does the appropriate query..
Extra info:
The new URL calls a view which renders a template, which again inherits the same main template as the previous template and resides in the same block...info.
Is there a better way to do this...?? Remember that my flash object calls a javascript callback function with a string parameter,so I have to maneuver from there somehow to the new template whilst passing the string parameter to the URL. Below in the code you can see the second parameter of the view function is called region. This is the parameter I need to pass in via javascript function.
THE URL (r'^crc_region_cities/?
Many Thanks
, 'crc_region_cities'),
The View
def crc_region_cities(request, region):
cities = City.objects.filter(region__exact=region)
return render_to_response('crc/crc_region_cities.html', {'cities': cities}, context_instance=RequestContext(request))
Many Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于所有浏览器问题,通常最好不要自己执行原始 AJAX。我建议使用 jQuery 来完成此任务(以及其他许多事情),因为它已经处理了所有的奇怪问题。
查看 jQuery AJAX API 页面,特别是 jQuery.get() 和jQuery.post() 用于传递参数的方法。
观察结果:我在分配给 href 的字符串上没有看到前导“/”,这意味着它相对于页面的原始 URL。这很容易引起问题。请记住,在调试 AJAX 内容时,带有 Firebug 扩展的 Firefox 是您的朋友。运行您的页面并查看标记为
Console
的选项卡。如果它报告错误,我不会感到惊讶。另一件值得关注的事情是:jQuery taconite 插件 是我用过的最甜蜜的 AJAX 扩展之一遭遇。一探究竟。
It's often best not to do raw AJAX yourself because of all of the browser issues. I recommend using jQuery for this (and a whole host of other things) because it already deals with all of the weirdness out there.
Take a look at the jQuery AJAX API page, in particular look at jQuery.get() and jQuery.post() for ways of passing params.
An observation: I don't see a leading '/' on the string you are assigning to href, meaning it's relative to the originating URL for the page. This could easily cause problems. Remember, Firefox w/Firebug extension is your friend when debugging AJAX stuff. Run you page and look at the tab labeled
Console
. I wouldn't be surprised if it's reporting an error.One other thing to look at: the jQuery taconite plugin is one of the sweetest AJAX extensions I've ever encountered. Check it out.