简体   繁体   中英

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....

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

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...

Expected : Loading page should work and after that control should shift to run command and then it should go to python_output.html page.../

The return statement will terminate the execution of the function so anything below it will never be reached.

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. 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

在此处输入图片说明

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

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.

Using JQuery, in the template file, you must call a function like this :

<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.
  • 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. Your view responds with the HTML page saying "please have some patience..." and javascript to handle what follows (see below).
  • 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). It responds with JSON, either by saying "status = processing..." or "status = done" with the result of the task.
  • 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.

Search for "django celery tutorial" and you'll find plenty of examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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