[英]Using variable generated from the Django view in the Javascript in Django template?
This variable below stores the processing in variable name proc : 下面的此变量将处理存储在变量名proc中 :
proc = subprocess.Popen(['sshpass', '-p', password, 'rsync', '-avz', '--info=progress2', source12, destination],
stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]
I am a form in a Django template. 我是Django模板中的表单。 Whenever the user presses the button Submit I want to use the variable proc and show the processing.
每当用户按下“ 提交 ”按钮时,我都想使用变量proc并显示处理过程。 I tried using the variable proc in Django template Javascript like this:
我尝试在Django模板Javascript中使用变量proc,如下所示:
function popUp() {
var a = "{{proc}}"
document.write(a)
}
</script>
But it shows only the empty page. 但它只显示空白页。 I have tested variable directly calling inside the html body tag it works fine but doesn't work with javascript.
我已经测试了直接在html body标签内调用的变量,它可以正常工作,但不适用于javascript。 What I am doing wrong?
我做错了什么?
I am calling the popUp function like this in the Django template like this: 我在Django模板中这样调用popUp函数,如下所示:
<form name="form1" action="/sync/" method="post" onsubmit="popUp()">
And, I am passing the variable proc in the Django template like this: 而且,我正在像这样在Django模板中传递变量proc :
return render_to_response('synced.html', {'final_result':final_list}, context_instance=RequestContext(request))
return render_to_response('thanks.html', {'proc':proc}, context_instance=RequestContext(request))
Edit: For the checking, instead of passing the variable into thanks.html I passed the variable proc into synced.html and used in template like this: 编辑:对于检查,不是将变量传递到thank.html中,而是将变量proc传递到synced.html中,并在模板中使用,如下所示:
<th>Files in the server:</th>
</tr>
<td>{{ proc }}</td>
<tr>
and my output is: 我的输出是:
receiving incremental file list 0 0% 0.00kB/s 0:00:00 (xfr#0, to-chk=0/1) sent 20 bytes received 56 bytes 6.08 bytes/sec total size is 8,640 speedup is 113.68
I suggest what you are doing is not wrong but there are better ways & technologies have been engineered to solve this usecase of yours. 我建议您在做什么并没有错,但是已经设计出了更好的方法和技术来解决您的这种用例。 Some of the things you can try -
您可以尝试的一些方法-
Not the answer you were looking for but hopefully this helps... 不是您要找的答案,但希望对您有帮助...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.