Search code examples
python-2.7templatesinputcontrollerodoo-8

Odoo template get value from input


In a custom template (website) I've added an input tag. I'd like to get the value of this tag in order to send it to the controller. By adding this to the URL, but I always get 'None' back.

<template id="InputTemp" inherit_id="website_sale.cart">
        <xpath expr="//div[@id='right_column']" position="after">
            <div class="col-lg-3 col-lg-offset-1 col-sm-3 col-md-3 text-muted" id="inputform">
                    <h3>Please enter value:</h3>
                    <label class="control-label" for="waardebon">Value</label>
                    <input type="text" name="value_input" class="form-control"/>
                    <a t-attf-href="/cart/#{str(value_input)}" class="btn btn-primary btn-lg mt8">Submit</a>
            </div>
        </xpath>
    </template>

Solution

  • Your t-attf-href is rendered before any data has been entered into the form field. To do it the way you are you need to update your href using javascript. In odoo9 you need to use requirejs syntax to load the proper libraries to run a post request to your controllers. If you are just using a get request then the following should work for your example.

    <template id="InputTemp" inherit_id="website_sale.cart">
        <xpath expr="//div[@id='right_column']" position="after">
            <div class="col-lg-3 col-lg-offset-1 col-sm-3 col-md-3 text-muted" id="inputform">
                    <h3>Please enter value:</h3>
                    <label class="control-label" for="waardebon">Value</label>
                    <input type="text" name="value_input" id="value_input" class="form-control"/>
                    <a id='submit-btn' t-attf-href="#" class="btn btn-primary btn-lg mt8">Submit</a>
                    <script>
                        var value_input = document.getElementById('value_input');
                        var submit_button = document.getElementById('submit-btn');
    
                        value_input.addEventListener('input', function(){
                            submit_button.href = "/cart/?input_value=" + value_input.value;
                        });
                    </script>
            </div>
        </xpath>
    </template>
    

    Here is an example controller.

    @http.route('/cart/', auth='public', website=True)
    def get_cart_vals(self, **kw):
        # YOUR VARIABLE value_input SHOULD BE AVAILABLE IN THE QUERY STRING 
        query_string = request.httprequest.query_string
        # PROCESS DATA AND LOAD THE RESPONSE TO THE USER OR REDIRECT HERE