1

I am developing the web using google app engine. I used jinja2 to send data from html to main. pays code. The problem is how to get the data from the javascript of html. Please let me know the correct answer. Thank you.

main.py

def get(self):
    db = connect_to_cloudsql()
    cursor = db.cursor()
    cursor.execute("set names utf8")
    cursor.execute("""select no, u_name, age, gender, U_adress, phone, car_num, penalty from User;""")

    data = cursor.fetchall() 
    array_list = []

    for row in data:
        temp = (row[0], str(row[1]), row[2], str(row[3]), str(row[4]), row[5], str(row[6]), row[7])
        array_list.append(temp)

    db.close()

    data = json.dumps(array_list)
    template_values = { 'data': data }

    template = JINJA_ENVIRONMENT.get_template('User.html')
    self.response.write(template.render(template_values))

and javascript code in html.

var table;

$(function() {
    $.get(function({ data }) {
        var jsonobj = JSON.parse({ data });

        table = $('#userindex').dataTable({ 
            "sPaginationType": "bootstrap",
            "sDom": "t<'row'<'col-xs-6 col-left'i><'col-xs-6 col-right'p>>",
            "bStateSave": false
        });

        $.each(jsonobj, function(key,value) {
            table.fnAddData(
                value[0],
                value[1],
                value[2],
                value[3],
                value[4],
                value[5],
                value[6],
                value[8]
            );
        });
    });
});

this is my error

datatables.min.js:16 GET https://firststep-2016.appspot.com/function%20(%7Bdata%7D)%7Bvar%20jsonobj%20=%20JSON.parse(%7Bdata%7D);table%20=%20$(' 404 ()

2 Answers 2

0

I found PyV8 accidentally today, which I think is what you need. It built a js run environment that you can run js code in python to get the text what you want. Here are the references.

http://www.silverna.org/blog/?p=252

https://code.google.com/p/pyv8/

Sign up to request clarification or add additional context in comments.

Comments

0

This's a good answer for sending json data to javascript with jinja. It maybe help you.

sending data as JSON object from Python to Javascript with Jinja

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.