[英]Multiple render in 1 function in views.py Django?
I am very new to Django... Using submit button i want to run a python file in background and display content on next page... But my python file takes some time to take out the result so in between i wanted to put a loading html page in between.... 我是Django的新手...使用提交按钮,我想在后台运行python文件并在下一页上显示内容...但是我的python文件需要一些时间才能取出结果,因此我想在两者之间放置一个正在加载html页面。
I have written some code which correctlty runs the python file but i am not able to incorporate the loading page in between... Take a look at my function in views.py 我已经编写了一些可以正确运行python文件的代码,但是我无法在两者之间合并加载页面...看一下views.py中的函数
def submit(request):
info = request.POST['info']
print('value is ', info)
filename = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
result = run(['python', filename, info], stdout= PIPE )
return render_to_response("loading.html")
run(['python', filename, info], stdout= PIPE )
return render(request, 'python_output.html', context)
ACTUAL RESULT: return render_to_response("loading.html") works but then the control does not shifts to run command... I want to run the loading html page and run the python file in background and when python file run finishes it should move to python _output.html page where output is displayed... 实际结果: return render_to_response(“ loading.html”)可以正常工作,但是控件不会转移到运行命令...我想运行正在加载的html页面,并在后台运行python文件,当python文件运行完成时,它应该移动到显示输出的python _output.html页面...
Expected : Loading page should work and after that control should shift to run command and then it should go to python_output.html page.../ 预期 :加载页面应该工作,并且在此之后控件应转到运行命令,然后应转到python_output.html页面... /
The return
statement will terminate the execution of the function so anything below it will never be reached. return
语句将终止函数的执行,因此,低于该函数的任何内容将永远无法到达。
You can use Javascript to show the loading icon and then use JQuery to run a GET request in the background where you call a custom view from Django that will output the result of the command. 您可以使用Javascript显示加载图标,然后使用JQuery在后台运行GET请求,并在其中调用Django的自定义视图,该视图将输出命令的结果。 When data is received you can then remove the icon and process the data as you want.
收到数据后,您可以删除图标并根据需要处理数据。
Basic Example : 基本示例:
Django
------
url(r'^command/', views.command, name='command'),
def command(request):
info = request.POST['info']
filename = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
result = run(['python', filename, info], stdout= PIPE
return result
Javascript
----------
<img id="loading-icon" src="loading.gif">
$.get("/command", function(text)
{
$("#loading-icon").remove();
process(text);
});
You need to understand the basic flow in Django 您需要了解Django中的基本流程
You can only add one return in your view. 您只能在视图中添加一个退货。 after the execution of the first return it goes to the response middleware so all other returns below are ignored.The loading can be done in javascript in frontend
执行完第一个返回后,它会转到响应中间件,因此下面的所有其他返回都将被忽略。可以在前端的javascript中完成加载
Try to use an ajax request when you load your first page (loading.html) to run the python file in background and when it is done, display the result via output.html. 加载第一页(loading.html)以在后台运行python文件时尝试使用ajax请求,完成后,通过output.html显示结果。
Using JQuery, in the template file, you must call a function like this : 使用JQuery,必须在模板文件中调用如下函数:
<script>
var url_file_to_run = "{% url "your_app:file_to_run_adress" 0 %}";
var url = "{% url "your_app:python_output" 0 %}";
$.ajax({
url: url_file_to_run,
}
}).done(function(data) {
$( location ).attr("href", url);
});
</script>
I hope i understand your problem. 希望我能理解您的问题。
What you want involves a bit more work than you might have expected: 您想要的工作比预期的要多:
Search for "django celery tutorial" and you'll find plenty of examples. 搜索“ django celery教程”,您将找到很多示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.