Django ajax没有刷新:django视图没有重定向或刷新页面 [英] Django ajax no refresh:django view without redirecting or refreshing a page

查看:704
本文介绍了Django ajax没有刷新:django视图没有重定向或刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果在网页上按下该按钮,我想执行一些Python代码。事情是,我不需要重定向或刷新页面。

解决方案

只需使用jQuery ajax,很容易在django
中查看



$ p

  def fun1(request):
user_input = request.GET.get('value')
#put你的代码在这里
返回HttpResponse('你想输出到web')

urls.py

  url(r'^ link-to-fun1 $',views.fun1),

html

 < html> 
< head>
< title>您的标题< / title>
< script type =text / javascriptsrc =/ media / js / jquery-1.10.2.min.js>< / script>
< script>
函数myFun(){
$ .get('link-to-fun1 /',{value:get_the_value_from_web},function(ret){
return ret; //你可以处理返回值ret这里
});
}
< / script>
< / head>
< body>




jQuery $ .get方法


I'd like to execute some Python code if that button is pressed on web.The thing is, I need the page not to redirect or refresh or anything.

解决方案

Just use jQuery ajax,it is easy to do

in django views.py

def fun1(request):
    user_input = request.GET.get('value')
    #put your code here
    return HttpResponse('what you want to output to web')

urls.py

url(r'^link-to-fun1$', views.fun1),

html

<html>
<head>
<title>Your title</title>
<script type="text/javascript" src="/media/js/jquery-1.10.2.min.js"></script>
<script>
function myFun() {
    $.get('link-to-fun1/',{"value":"get_the_value_from_web"},function(ret) {
                return ret;//you can handle with return value ret here
            });
}
</script>
</head>
<body>

More see jQuery $.get method

这篇关于Django ajax没有刷新:django视图没有重定向或刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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