Development Book V17: Manage website record

The records of everything on a website are called website records. Everything that is produced or checked out on the website's front end will be kept on the back end. We can see that all the details of the website are stored on the backend. We can examine how to construct a form for a website and how to save that data in the backend.

Create a model and its view


<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
   <!--    Tree View-->
   <record id="survey_form_view_tree" model="ir.ui.view">
   <field name="name">survey.form.view.tree</field>
   <field name="model">survey.form</field>
   <field name="arch" type="xml">
       <tree create="false">
           <field name="name"/>
           <field name="dob"/>
           <field name="qualification"/>
           <field name="phone"/>
           <field name="email"/>
       </tree>
   </field>
</record>
<!--    Record Action-->
   <record id="survey_form_action" model="ir.actions.act_window">
       <field name="name">Survey</field>
       <field name="res_model">survey.form</field>
       <field name="view_mode">tree,form</field>
   </record>
</odoo>
                                

Then construct a menu called Survey.


 <record id="website_survey_menu" model="website.menu">
       <field name="name">Survey</field>
       <field name="url">/survey</field>
       <field name="parent_id" ref="website.main_menu"/>
       <field name="sequence" type="int">90</field>
   </record>
                                

By clicking on the menu we want to redirect to the form view. So we need to define a route function and the template.


from odoo import http

class Survey(http.Controller):
   @http.route('/survey', auth='public', website=True)
   def survey(self):
      return request.render(module_name.survey_form_template')

                                

<?xml version="1.0" encoding="UTF-8" ?>
<odoo>
   <template id="survey_form_template" name="Survey Form">
       <t t-call="website.layout">
           <div class="container s_website_form">
               <h3>Survey Form</h3>
               <t t-if="submitted">
                   <h3 class="alert alert-success mt16 mb16">
                       <i class="fa fa-thumbs-up"/>
                       Survey submitted successfully
                   </h3>
                   <a href="/survey_form">Submit another survey</a>
               </t>
               <t t-else="">
                   <div class="row">
                       <div class="col-6">
                           <form id="form_survey_form"
                                 action="/survey_form" method="POST">
                               <input type="hidden" name="csrf_token"
                                      t-att-value="request.csrf_token()"/>
                               <div class="s_website_form_rows row s_col_no_bgcolor">
                                   <div class="form-group col-12">
                                       <label for="name">Name</label>
                                       <input type="text" class="form-control"
                                              name="name"
                                              placeholder="Enter your name"
                                              required="required"/>
                                   </div>
                                   <div class="form-group col-12">
                                       <label for="email">Email</label>
                                       <input type="email" class="form-control"
                                              name="email"
                                              placeholder="Enter your mail-id"
                                              required="required"/>
                                   </div>
                                   <div class="form-group col-12">
                                       <label for="phone">Phone</label>
                                       <input type="number"
                                              class="form-control"
                                              name="phone"
                                              placeholder="Enter your name"
                                              required="required"/>
                                   </div>
                                   <div class="form-group col-12">
                                       <label for="dob">DOB</label>
                                       <input type="date" class="form-control"
                                              name="dob"
                                              placeholder="dd/mm/yyyy"/>
                                   </div>
                                   <div class="form-group col-12">
                                       <label for="qualification">
                                           Qualification
                                       </label>
                                       <select class="form-control"
                                               name="qualification">
                                           <option value="">Select</option>
                                           <option value="pg">Post Graduation
                                           </option>
                                           <option value="ug">Graduation
                                           </option>
                                           <option value="higher_secondary">
                                               Higher Secondary
                                           </option>
                                           <option value="secondary">
                                               Secondary
                                           </option>
                                       </select>
                                   </div>
                                   <div class="form-group col-12">
                                       <input type="submit"
                                              class="btn btn-primary"
                                              value="Submit"/>
                                   </div>
                               </div>
                           </form>
                       </div>
                   </div>
               </t>
           </div>
       </t>
   </template>
</odoo>

                                

Next, we need to define another route function that creates a record in the backend when we submit the form.


@http.route(['/survey_form'], type='http', auth="user", website=True)
def survey_form(self, **post):
   name = post.get('name')
   email = post.get('email')
   phone = post.get('phone')
   if name and email and phone:
      request.env['survey.form'].sudo().create({
         'name': name,
         'email': email,
         'phone': phone,
         'dob': post.get('dob'),
         'qualification': post.get('qualification')
      })
      return request.redirect('/survey_form?submitted=1')
   return request.render(module_name.survey_form_template',
                         {'submitted': post.get('submitted', False)})

                                

When the survey_form triggers a new record will be created in the backend with input values in the frontend.

whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message