Development Book V18: Manage Website Records

All activities and content generated on a website are referred to as website records. Any interaction or transaction that occurs on the website's front end is recorded and stored in the backend. This allows us to access and manage all website-related data from the backend. In this context, we can explore how to build a form on the website and ensure that the submitted data is properly saved and managed in the backend.


<?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">
           <list string="Survey">
               <field name="name"/>
               <field name="dob"/>
               <field name="qualification"/>
               <field name="phone"/>
               <field name="email"/>
           </list>
       </field>
   </record>

   <!-- Record Action -- >
   <record id="action_survey_form" model="ir.actions.act_window">
       <field name="name">Survey</field>
       <field name="res_model">survey.form</field>
       <field name="view_mode">list,form</field>
       <field name="help" type="html">
           <p class="o_view_nocontent_smiling_face">
               Create your first survey record
           </p>
       </field>
   </record>
</odoo>

Next, create a menu item labeled "Survey."


<!-- Website Menu: Survey -->
<record id="menu_website_survey" model="website.menu">
   <field name="name">Survey</field>
   <field name="url">/survey</field>
   <field name="parent_id" ref="website.main_menu"/>
   <field name="sequence">90</field>
</record>

To display the form when the menu is clicked, you need to define a corresponding route handler and create a website template that renders the form.


from odoo import http
from odoo.http import request

class Survey(http.Controller):

    @http.route('/survey', auth='public', website=True)
    def survey(self, **kwargs):
        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">
          <div class="alert alert-success mt16 mb16">
            <i class="fa fa-thumbs-up"/> Survey submitted successfully
          </div>
          <a href="/survey">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 email"
                           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 phone number"
                           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>

                  <t t-if="not hide_button">
                    <div class="form-group col-12">
                      <input type="submit" class="btn btn-primary" value="Submit"/>
                    </div>
                  </t>

                  <t t-if="hide_button">
                    <div class="form-group col-12">
                      <div class="alert alert-warning">
                        Submissions are closed after 3:30 PM.
                      </div>
                    </div>
                  </t>

                </div> 
              </form>
            </div>
          </div>
        </t>

      </div>
    </t>
  </template>
</odoo>

Next, you need to define a route handler that processes the form submission and creates a corresponding record in the backend.

from odoo import http
from odoo.http import request
from datetime import datetime


class Survey(http.Controller):


   @http.route('/survey_form', type='http', auth="user", website=True, csrf=True)
   def survey_form(self, **post):
       name = post.get('name')
       email = post.get('email')
       phone = post.get('phone')
       dob = post.get('dob')
       qualification = post.get('qualification')


       # Optional: Prevent submission after 3:30 PM
       current_time = datetime.now().time()
       if current_time >= datetime.strptime("15:30", "%H:%M").time():
           return request.render('your_module_name.survey_form_template', {
               'submitted': False,
               'hide_button': True,
               'error': 'Submissions are closed after 3:30 PM.'
           })


       if name and email and phone:
           request.env['survey.form'].sudo().create({
               'name': name,
               'email': email,
               'phone': phone,
               'dob': dob,
               'qualification': qualification
           })
           return request.redirect('/survey?submitted=1')


       return request.render('your_module_name.survey_form_template', {
           'submitted': post.get('submitted', False),
           'hide_button': False,
       })

When the /survey_form route is triggered, a new record is created in the backend using the input values submitted from the frontend form.

whatsapp_icon
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