简体   繁体   English

在views.py Django中1函数中的多个渲染

[英]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: 您想要的工作比预期的要多:

  • Install a background task framework like celery (and a queue like Redis or RabbitMQ to store tasks) that will fetch tasks from the queue and process them. 安装诸如celery的后台任务框架(以及诸如Redis或RabbitMQ的队列来存储任务),该框架将从队列中获取任务并进行处理。
  • Your initial view needs to start a background task. 您的初始视图需要启动后台任务。
  • You want to keep track of the task_id for this task, either by returning it in your response to the user or saving it in the session of the user. 您想要跟踪此任务的task_id,方法是在对用户的响应中返回它,或者将其保存在用户会话中。 Your view responds with the HTML page saying "please have some patience..." and javascript to handle what follows (see below). 您的视图以HTML页面回应,请说“请耐心等待...”和javascript处理以下内容(请参见下文)。
  • Then you need a view that can check the status of the task (based on either the task_id passed in the query or saved in the current session). 然后,您需要一个可以检查任务状态的视图(基于查询中传递的task_id或当前会话中保存的task_id)。 It responds with JSON, either by saying "status = processing..." or "status = done" with the result of the task. 它使用任务结果返回“状态=处理中...”或“状态=完成”,以JSON进行响应。
  • In the HTML page, you need javascript that queries that view at regular intervals, until the status is "done" and then processes the result of the task to update the HTML of the page. 在HTML页面中,您需要Javascript定期查询该视图,直到状态为“完成”,然后处理任务结果以更新页面的HTML。

Search for "django celery tutorial" and you'll find plenty of examples. 搜索“ django celery教程”,您将找到很多示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM