如何在Django视图中访问jQuery.ajax()获取参数 [英] how to access jQuery.ajax() get parameters in Django views
问题描述
我不知道如何在Django中访问get参数
我的代码看起来像:
Jquery& html:
< div id =browser>
< ul>
{%comment%}
这是每个ctg的脚本。每个脚本淡出#astream,在#stream_loading中淡化,然后应该在ajax调用中基于GET参数显示带有新值的#astream
它们不会工作,但首先我需要与我的GET参数进行交互view.py
{%endcomment%}
{ctg%ctg%}
< script type =text / javascriptcharset =utf-8> ;
(function($){
$(document).ready(function(){
$(#stream_loading)hide()
$ #browse _ {{ctg}})。点击(function(){
$(#astream)。fadeOut()
$(#stream_loading)fadeIn )
$ .ajax({
type:GET,
url:/ {{defo}} /?param = {{ctg}},
success:function(){
$(#stream_loading)。fadeOut()
$(#astream)。fadeIn()
}
});
});
});
})(jQuery);
< / script>
< li>< a id =browse _ {{ctg}}title ={{ctg}}> {{ctg}}< / a>< / li>
{%endfor%}
< / ul>
< / div>
< div id =astream>
{{ajaxGet}} #just查看呈现的内容
{%includeastream.html%}
< / div>
< div id =stream_loading>
加载流,请稍候...
< / div>
Django:
code> @https_off
def index(request,template ='index.html'):
request.session.set_test_cookie()
path = request.META.get('PATH_INFO')
defo = path [1:path [1:]。find('/')+ 1]
request.session ['defo'] = defo
defo = request.session ['defo']
#构建由-pub_date排序的流
import itertools
chained = itertools.chain(
model1.objects.order_by(' - pub_date')。filter(),
model2.objects.order_by(' - pub_date')。filter(),
)
stream = sorted(chained,key = lambda x:x.pub_date,reverse = True)
ajaxGet = request.GET.get('param','dummy')
如果request.is_ajax():
template ='astream.html'
ajaxGet = request.GET.get('param',False)
renderParams = { 'defo':defo,'stream':stream,'ajaxGet':ajaxGet}
返回render_to_response(template,renderParams,context_instance = RequestContext(request))
然后我尝试显示在我的模板中
{{ajaxGet}}
但是每次都被渲染为虚拟
在firebug中,我可以看到使用正确的键和值获取请求。 p>
我想念什么?
谢谢
在执行这种Ajax时,人们经常会遇到这种情况,这并不妨碍链接/按钮的默认操作。所以你的Ajax功能从来没有机会被触发,你在Django代码中看到的请求是由正常的页面加载引起的 - 这就是为什么 is_ajax()
是$。
给点击
处理程序一个参数事件
,并在函数结尾调用 event.preventDefault();
。
I recently started to learn jQuery and right now I am playing around with .ajax() function.
I cannot figure out how to access the get parameters in Django.
My code looks like:
Jquery & html:
<div id="browser">
<ul>
{% comment %}
Theres a script for each ctg. Each script fades out #astream, fades in #stream_loading and then it should display #astream with new values based on the GET param in ajax call
Prolly it wont work, but first I need to interact with the GET param in my views.py
{% endcomment %}
{% for ctg in ctgs %}
<script type="text/javascript" charset="utf-8">
(function($) {
$(document).ready(function() {
$("#stream_loading").hide()
$("#browse_{{ctg}}").click(function() {
$("#astream").fadeOut()
$("#stream_loading").fadeIn()
$.ajax({
type: "GET",
url: "/{{defo}}/?param={{ctg}}",
success: function() {
$("#stream_loading").fadeOut()
$("#astream").fadeIn()
}
});
});
});
})(jQuery);
</script>
<li><a id="browse_{{ctg}}" title="{{ctg}}">{{ctg}}</a></li>
{% endfor %}
</ul>
</div>
<div id="astream">
{{ajaxGet}} #just to see whats rendered
{% include "astream.html" %}
</div>
<div id="stream_loading">
loading stream, please wait ...
</div>
Django:
@https_off
def index(request, template='index.html'):
request.session.set_test_cookie()
path=request.META.get('PATH_INFO')
defo=path[1:path[1:].find('/')+1]
request.session['defo']=defo
defo=request.session['defo']
# build the stream sorted by -pub_date
import itertools
chained=itertools.chain(
model1.objects.order_by('-pub_date').filter(),
model2.objects.order_by('-pub_date').filter(),
)
stream=sorted(chained, key=lambda x: x.pub_date, reverse=True)
ajaxGet=request.GET.get('param','dummy')
if request.is_ajax():
template='astream.html'
ajaxGet=request.GET.get('param',False)
renderParams={'defo':defo, 'stream':stream, 'ajaxGet':ajaxGet}
return render_to_response(template, renderParams, context_instance=RequestContext(request))
Then I try to show it up in my template
{{ ajaxGet }}
But everytime is rendered as 'dummy'
In firebug I can see get requests with proper key and value.
What do I miss here?
Thanks
There is a frequent gotcha that people often fall into when doing this kind of Ajax, and that is not preventing the default action of the link/button. So your Ajax function never has a chance to fire, and the request that you're seeing in the Django code is caused by the normal page load - which is why is_ajax()
is false.
Give the click
handler a parameter, event
, and call event.preventDefault();
at the end of the function.
这篇关于如何在Django视图中访问jQuery.ajax()获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!