0

test.html

{% extends 'base.html' %}

{% block extra_js %}
<script language="Javascript">
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}
</script>

{% endblock %}

{% block main1 %}
<form name="f1">
  <p>word: <input name="word" type="text">  
  <input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/site_media/rt.py")'></p>
  <div id="result"></div>
</form>
{% endblock main1 %}

rt.py

def ret():
    i=10
    return i

so when I click on Go button I want get i value 10 in div tag. but the output is like def ret(): i=10 return i

its just read rt.py and show on webpage.

1 Answer 1

1

What do you expect? That the string 10 appears in the page?

It will not, because you have the following problems:

  1. your web server is not configured to interpret Python programs (as it just returns the source code)
  2. your python code will not write anything to standard output, and a web server, after it ended processing the code, will usually return the standard output of the program to the HTTP client

Now, I suspect that you use Django, so it could not be a problem of web server but just a configuration problem. In this case, please follow the Django tutorial that corresponds to your Django version, and you'll understand that you have to configure an URL, map it to an application and write there your response.

If you don't use Django, just fix the web server and your python code as I (shortly) explained.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.