Tuesday, 26 June 2018

Odoo get web form or template value in controller

In this article I am going to discuss how to get web form values such as input type text, radio, date and checkbox in odoo controller. To achieve this we need to create a form tag in our template and in that form we have to create some input elements such as a date picker, two checkbox and one submit button. Now on the click of submit button we want form data in out controller.




Problem Statement 1

On submit button click get all form data for example (one date picker value and two checkbox value). In this scenario we want to get all form data in our controller. Remember that the form data will be fetched in the form of python dictionary in our controller. In our second scenario we will get only checkbox value in our controller.

template.xml



<openerp>
 <data>
    <template id="test_form">              
        <t t-call="website.layout">  
        <script type="text/javascript" src="/test_workflow/static/src/js/jquery.min.js"></script>
       
            <body> 
                <div class="container">   
                    <div class="page">
                        <div class="row">
                            <form>                  
                              <input type="date" name="start_date"/>

                              <input type="checkbox" name="critical" value="Critical"></input>      
                              <input type="checkbox" name="minor" value="Minor"></input>                 
                              <input type="submit" value="Submit" ></input>
                            </form>
                        </div>
                    </div> 
                </div>          
            </body>

        </t>
    </template>
    </data>
</openerp>      
 

controller.py


class test_controller(http.Controller):

 @http.route('/test1/<self_id>', auth='user', website=True)
 def test1(self,self_id,**kw):
  print('>>>>>>>>>>>>>>test123', kw)
  return http.request.render('test_workflow.test_form', {
   'num_list':[1,2,3,4,5,6,7],
 })              
 


Problem Statement 2

In our second scenario we will manipulate only two checkbox values which we have got on submit button. To achieve this functionality we will modify our controller code like below.


@http.route('/test1/<self_id>', auth='user', website=True)
def test1(self,self_id,**kw):
    print('>>>>>>>>>>>>>>test123', kw)
    dictfilt = lambda x, y: dict([ (i,x[i]) for i in x if i in set(y)])
    wanted_keys = ("minor","critical")
    result = dictfilt(kw, wanted_keys)
    print('>>>>>>>>>>>>>>test', result.values())
    return http.request.render('test_workflow.test_form', {
 'num_list':[1,2,3,4,5,6,7],
 })              
 


As you can see that now we have only two checkbox values in our controller as per our need.

No comments:

Post a Comment