tags:

views:

543

answers:

2

I have a web2py application and am using jQuery to validate data. I want to write Python code inside jQuery. How can I do that?

if($("#myform_parent_id")[0].value != '' 
    && $("#myform_parent_birthdate")[0].value != ''
    && 
      // Here I want to write Python code
  ..
}

Because when i make this code inside controller and then return the form it is reset selected values.

A: 

As no browser will understand Python, you'll have to make an Ajax call to the server if you really want to use Python. This will probably complicate things for you, if only as Ajax calls are asynchronous by definition. But jQuery does support it.

Alternatively, to keep it all within the browser, you need JavaScript-only. If you can't translate your Python to JavaScript yourself, you could take a look at automated conversions.

Arjan
+1  A: 

If you define this code in the view (instead of the controller) then you can use Python code within {{ }} blocks.

Plumo