Wilson Canda
Wilson Canda

Reputation: 444

Retrieving variable from Javascript function in python Appengine

I am very new to all of the web technologies involved in this question, so please bear with me :) I am currently busy with my first appengine project. What I am trying to do is manipulate a value produced within a JS function (which is in my .html file) using python code. My problem is, I don't know how to "pick it up".

function createSomething(some, thing) {
    var something = some + thing; //I need to retrieve that "something" value 
}

To elaborate a little, I am able to retrieve things that have been entered through an HTML form:

<textarea name="content" rows="3" cols="60"></textarea>
<input type="submit" value="Submit Something">

By using python code that looks like this:

entry.content = self.request.get('content')

But how would one go about doing something similar with a value which is produced within some JS function? (I am using python + django + html + js on appengine, to just try out the basics)

Upvotes: 1

Views: 596

Answers (1)

Brandon Taylor
Brandon Taylor

Reputation: 34593

You would have to send the value of something to the server-side via a GET or POST request, depending on your needs. If you're trying to do that without reloading the page, you'll need to use an Ajax request. Your Python code on the server side has no way of accessing a client-side value unless you send the client-side value to the server. Something similar to:

# your-javascript-file.js or a script block on your page

function createSomething(some, thing) {
    if (some !== undefined && thing !== undefined) {
        $.POST('/a-django-url-pattern/', {'something': some + thing},
            function(responseData) {
            // do something else with the responseData, etc
        });
    }
}

# your Django view
def your_view_function(request):
    if request.method == 'POST':
        something = request.POST.get('something')
        if something:
            # do whatever

    return render(request, 'a-template.html')

Upvotes: 2

Related Questions