通过动态JavaScript变量的Django / Python的 [英] Pass Dynamic Javascript Variable to Django/Python
问题描述
我已经看过了一些答案,和其他网站,但没有回答我的具体问题。我有+和网页 - 按钮,这应该增加一个称为pieFact变量。此变量必须动态地更新,而无需刷新页面。它然后应的值改变时传递给我的Django视图。这将被用于更新饼图的大小在web映射。我有以下几点:
I have looked at a number of answers and other websites, but none answer my specific question. I have a webpage with "+" and "-" buttons, which should increment a variable called "pieFact". This variable must be updated dynamically without having to refresh the page. It should then be passed to my Django view each time the value is changed. This will be used to update the size of pie charts in a web map. I have the following:
<button type="button" id=bttnMinus onclick="pieFact=pieFact*0.9">-</button>
<button type="button" id=bttnPlus onclick="pieFact=pieFact*1.1">+</button></td>
<script type="text.javascript">
var pieFact=0;
</script>
如何传递pieFact来的Django的价值?根据我有限的知识,我想我可能要使用AJAX后/搞定了。
How can I pass the value of "pieFact" to Django? Based on my limited knowledge, I think I may have to use AJAX post/get.
推荐答案
为了以防止刷新页面,是的,你需要的AJAX。我平时不喜欢但是,建议在回答太多的图书馆,在被轻易跨浏览器兼容的利益,我建议使用的 jQuery的。
In order to keep from refreshing the page, yes, you will need AJAX. I usually don't like to suggest libraries too much in answers, however, in the interest of being easily cross-browser compatible, I would suggest the use of jQuery.
<html>
...
<head>
<script>
var URL = "{% url 'my_view_that_updates_pieFact' %}";
</script>
</head>
...
稍后...
您需要POST或GET数据通过AJAX服务器。为了更平安,每当我需要将数据发送到我使用POST的服务器。 jQuery提供了 $。员额()方便的功能,AJAX数据通过POST的URL。这三个参数的URL,发送数据(如JavaScript对象;认为Python字典,如果你不是太熟悉JavaScript),一旦服务器的回调函数发送回一个响应
Later on...
You'll need to either POST or GET the data to the server via AJAX. To be more RESTful, whenever I need to send data to the server I use POST. jQuery provides the $.post() convenience function to AJAX data to a url via POST. The three parameters are the URL, the data to send (as a JavaScript object; think python dictionaries if you're not too familiar with JavaScript), and a callback function once the server sends back a response.
<script>
function updatePieFact(){
var data = {'pieFact': pieFact};
$.post(URL, data, function(response){
if(response === 'success'){ alert('Yay!'); }
else{ alert('Error! :('); }
});
}
在。点击()
的功能基本上是相同的事情在HTML属性指定点击数
。这两个点击事件更新 pieFact
你所期望的,然后调用 updatePieFact()
发送的值 pieFact
到服务器。
The .click()
functions are basically the same thing as specifying onlick
in the html attribute. Both click events update pieFact
as you would expect and then call updatePieFact()
to send the value of pieFact
to the server.
$(document).ready(function(){
$('#bttnMinus').click(function(){
pieFact *= 0.9;
updatePieFact();
});
$('#bttnPlus').click(function(){
pieFact *= 1.1;
updatePieFact();
});
});
</script>
在views.py
由于我使用了 $。员额()
函数在JavaScript中,即Django的是要接收都将有<$ C的方法要求$ C>POST,所以我检查,以确保该方法确实 POST
(这意味着如果有人访问的URL,这查看与像一个GET请求,他们将不会更新任何东西)。有一次,我看到的请求,事实上,一个 POST
,我检查,看看是否的关键pieFact
在字典 request.POST
。
In views.py
Since I've used the $.post()
function in the JavaScript, the request that Django is going to receive is going to have a method of "POST"
, so I check to make sure that the method is indeed POST
(this means that if someone visits the URL for this view with something like a GET request, they won't update anything). Once I see that the request is, in fact, a POST
, I check to see if the key 'pieFact'
is in the dict request.POST
.
记得我在JavaScript中设置变量数据
到 {'pieFact:pieFact}
?的JavaScript只是成为request.POST Python字典。因此,如果在JavaScript的我,而不是用 VAR数据= {'你好':pieFact};
,然后我会检查如果'你好'在request.POST
代替。有一次,我看到 pieFact
是request.POST字典,我可以得到它的值,然后用它做什么。如果一切顺利,我返回一个的Htt presponse
与字符串成功
。这与相关JavaScript中的检查:如果(响应==='成功')
Remember when I set the variable data
in the javascript to {'pieFact': pieFact}
? That javascript just becomes the request.POST python dictionary. So, if in the javascript I had instead used var data = {'hello': pieFact};
, then I would be checking if 'hello' in request.POST
instead. Once I see that pieFact
is in the request.POST dictionary, I can get its value and then do something with it. If everything is successful, I return an HttpResponse
with the string 'success'
. This correlates with the check in javascript: if(response === 'success')
.
def my_view_that_updates_pieFact(request):
if request.method == 'POST':
if 'pieFact' in request.POST:
pieFact = request.POST['pieFact']
# doSomething with pieFact here...
return HttpResponse('success') # if everything is OK
# nothing went well
return HttpRepsonse('FAIL!!!!!')
希望这将让你在正确的方向。
Hopefully that will get you pointed in the right direction.
这篇关于通过动态JavaScript变量的Django / Python的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!