在我的views.py,Django中使用ajax请求的结果 [英] Use the result of an ajax request in my views.py, Django

查看:143
本文介绍了在我的views.py,Django中使用ajax请求的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

感谢javascript函数和ajax请求,我有一个计数,指示用户在使用我的应用程序时所产生的积分数。他可以在他播放的页面上看到这个数字。



现在,我想做的是将这个数字传递给我的views.py,以便它修改了我的用户的Score对象。



我解释。感谢这个函数,我得到了这个数:

  var count = parseInt(0); 

setInterval(function(){
$ .ajax({
type:GET,
url:http:// myxml,
成功:Escore
});
},60000);

函数Escore(xml){
$(xml).find(user)。each(function(){

if($(this) .attr(id)=== id){
count + = parseInt($(this).attr(count));
$(。PlayerScore)。html )
}
});
}

显示在我的html中:

 < div class =PlayerScore> < / DIV> 

现在,我想修改我的用户的对象得分。每一分钟,我有一个请求,给我的玩家在我的div的点数。为了修改我的views.py中的request.user.userprofile.score,我该怎么做这个计数?



我希望我的问题不会太混乱。任何帮助将是非常受欢迎的。谢谢!

解决方案

Hm如果我明白这个问题,您需要执行以下操作:


  1. 创建您的视图功能,并在附加到此视图功能的URL.conf中创建一个URL模式。像/ points / save /


  2. 此功能可能需要用户的点数和用户ID(以便您可以对特定用户进行更改数据库)


  3. 您需要创建ajax请求,将点数据和用户ID发送到指向您的视图功能的URL。通过GET / POST发送


  4. 您的查看功能将从从jquery ajax调用发送的用户ID查找用户,然后使用从ajax调用


以下是一些帮助指南



使用Jquery和Django的Ajax



Django和Jquery


Thanks to a javascript function and an ajax request, I have a count indicating the number of points a user makes when he uses my app. He can see that count on the page where he 'plays'.

Now, what I would like to do, is to pass this number into my views.py so that it modifies the object "Score" of my user.

I explain. Thanks to this function, I get the count:

       var count = parseInt(0);

 setInterval(function() {
   $.ajax({
       type: "GET",
       url: "http://myxml",
       success: Escore
   });
  }, 60000);

function Escore(xml){
   $(xml).find("user").each(function() {

           if($(this).attr("id") === id ) {
               count += parseInt($(this).attr("count"));
               $(".PlayerScore").html(count)
           }
       });
 }

displayed in my html:

   <div class="PlayerScore"> </div>

Now, I would like to modify the object "score" of my user thanks to that. Every minute, I have a request that gives me the number of points the player makes in my div. How can I take this count in order to modify my "request.user.userprofile.score" in my views.py?

I hope my question is not too confusing. Any help would be really welcome. thanks!

解决方案

Hm If I understand the question, you need to do the following:

  1. Create your view function, and create a URL pattern in URL.conf that attaches to this view function. Something like /points/save/

  2. This function will likely require the points of the user and the user ID (so you can make changes to the specific user in the database)

  3. You need to make an ajax request that sends the point data and the user ID To the URL that points to your view function. Sent through GET/POST

  4. Your view function will lookup the user from the user id sent from jquery ajax call, then edit the user points with the points sent from the ajax call

Here are a few guides to help

Ajax with Jquery and Django

Django and Jquery

这篇关于在我的views.py,Django中使用ajax请求的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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