Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print output to webpage Django

Tags:

django

I currently have a long running script which produces various output. What I want to do is have this script run when a button on my webapp is pressed, and for the output to be displayed in real time in a text area on the webpage. I was wondering the simplest way to achieve this using Django.

like image 637
user3277112 Avatar asked Mar 03 '14 17:03

user3277112


People also ask

Can you print in Django?

Yes, you can print directly to a printer from Django (or any web app) using this method.

How connect Django with HTML?

In the Django Intro page, we learned that the result should be in HTML, and it should be created in a template, so let's do that. Create a templates folder inside the members folder, and create a HTML file named myfirst.html .


1 Answers

If you are talking about real time output then you need to use AJAX.

To set off the script, in the webpage you can have a button that sends an AJAX request.

function ajax_call_model(data_JSON_Request, object_id){
    $(function jQuery_AJAX_model(){
        $.ajax({
          type: 'GET',
          url: '/ajax_request/',
          data: something,
          datatype: "json",
          success: function(data) {
            $("#output_id").html(data);
          },//success
          error: function() {alert("failed...");}
        });//.ajax
      });//jQuery_AJAX
    };//ajax_call

In views you will have something like this:

def ajax_request(request):
    something = request.GET.get('something', '')# Receives from AJAX
    output = #Does something with the request
    jsonDump = json.dumps(str(output))

    return HttpResponse(jsonDump, content_type='application/json') 
like image 132
DGDD Avatar answered Sep 20 '22 01:09

DGDD