I need to create a form in my Odoo website so external users can register for a newsletter that is sent from my Odoo. So far I have created a website template and a controller. Thus far I have managed to display the form and do a Post as well as preload some additional many2many fields I need to fill the form but the data is not saved to the database when I hit submit. I have seen the website_portal and website_hr_recruitment models that have forms for people to fill out but I can't figure out where is the data saved and how it is done.
I include the following code from my own controller for reference:
My controller:
@http.route('/contact/register', type='http', auth='public', website='true')
def register(self, redirect=None, **post):
areas_interest = request.env['iica_contacts.area_interest'].sudo().search([])
topics_interest = request.env['iica_contacts.topic_interest'].sudo().search([])
products_interest = request.env['iica_contacts.products_interest'].sudo().search([])
countries = request.env['res.country'].sudo().search([])
if post:
return request.redirect('/contact/register/success')
values = {
'areas_interest' : areas_interest,
'topics_interest' : topics_interest,
'products_interest' : products_interest,
'countries' : countries,
}
return request.render("iica_contacts.register", values)
My Template:
<template id="register">
<t t-call="website.layout">
<t t-set="title">Register</t>
<form action="/contact/register" method="post">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
<div class="container">
<div class="row">
<div class="col-md-6 form-group">
<label for="first_name">First name:</label>
<input type="text" name="first_name" class="form-control" t-att-value="first_name" />
</div>
<div class="col-md-6 form-group">
<label for="last_name">Last name:</label>
<input type="text" name="last_name" class="form-control" t-att-value="last_name" />
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">Location information</div>
<div class="panel-body">
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label" for="country_id">Country:</label>
<select name="country_id" class="form-control">
<option value=""> -- Select country -- </option>
<t t-foreach="countries" t-as="country">
<option t-att-value="country.id">
<t t-esc="country.name" />
</option>
</t>
</select>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="company">Company:</label>
<input type="text" name="company" class="form-control" t-att-value="company" />
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label" for="sector">Sector:</label>
<select name="sector" class="form-control">
<option value=""> -- Select sector -- </option>
<option value="1">Public</option>
<option value="2">Private</option>
<option value="3">ONG</option>
<option value="4">International Organization</option>
<option value="5">Financial</option>
<option value="6">Other</option>
</select>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label" for="language">Language:</label>
<select name="sector" class="form-control">
<option value=""> -- Select language -- </option>
<option value="1">Spanish</option>
<option value="2">English</option>
</select>
</div>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">Contact information</div>
<div class="panel-body">
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label" for="email">Email:</label>
<input type="email" name="email" class="form-control" />
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label" for="phone">Phone number:</label>
<input type="tel" name="phone" class="form-control" />
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label class="control-label" for="contact_preference">Contact preference:</label>
<select name="contact_preference" class="form-control">
<option value=""> -- Select preference -- </option>
<option value="1">Telephone</option>
<option value="2">Email</option>
<option value="3">Text message (SMS)</option>
<option value="2">All of the above</option>
<option value="2">Dont contact me</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<ul class="nav nav-tabs">
<li class="active"><a href="#products-interest" data-toggle="tab">Products of interest</a></li>
<li><a href="#areas-interest" data-toggle="tab">Areas of interest</a></li>
<li><a href="#topics-interest" data-toggle="tab">Topics of interest</a></li>
</ul>
<div class="tab-content">
<div id="products-interest" class="tab-pane fade in active">
<h3>Products of interest</h3>
<select name="products_interest" multiple="multiple" class="form-control">
<t t-foreach="products_interest" t-as="product">
<option t-att-value="product.id">
<t t-esc="product.name" />
</option>
</t>
</select>
</div>
<div id="areas-interest" class="tab-pane fade">
<h3>Areas of interest</h3>
<select name="areas_interest" multiple="multiple" class="form-control">
<t t-foreach="areas_interest" t-as="area">
<option t-att-value="area.id">
<t t-esc="area.name" />
</option>
</t>
</select>
</div>
<div id="topics-interest" class="tab-pane fade">
<h3>Topics of interest</h3>
<select name="topics_interest" multiple="multiple" class="form-control">
<t t-foreach="topics_interest" t-as="topic">
<option t-att-value="topic.id">
<t t-esc="topic.name" />
</option>
</t>
</select>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<button type="submit" class="btn btn-default btn-primary">
Register <span class="fa fa-long-arrow-right" />
</button>
</div>
</div>
</div>
</form>
</t>
I will really appreciate any help.
After investigating and trying a lot of stuff I managed to do this the following way:
def register(self, redirect=None, **post):
areas_interest = request.env['iica_contacts.area_interest'].sudo().search([])
topics_interest = request.env['iica_contacts.topic_interest'].sudo().search([])
products_interest = request.env['iica_contacts.products_interest'].sudo().search([])
countries = request.env['res.country'].sudo().search([])
values = {
'areas_interest' : areas_interest,
'topics_interest' : topics_interest,
'products_interest' : products_interest,
'countries' : countries,
}
if post:
error_message, valid = self._validate_form(post)
if valid == 1:
self._process_registration(post)
return request.redirect('/contact/register/success')
else:
return request.redirect('/contact/register/error')
else:
contact = request.env['iica_contacts.contact']
values.update({
'contact' : contact,
})
return request.render("iica_contacts.register", values)
The function that inserts to the model:
def _process_registration(self, post):
request.env['iica_contacts.contact'].create({
'name' : post.get('name'),
'company' : post.get('company'),
'country_id': post.get('country_id'),
'sector' : post.get('sector'),
'language' : post.get('language'),
'email' : post.get('email'),
'phone' : post.get('phone'),
'area_interest_ids' : self._process_many2may(request.httprequest.form.getlist('areas_interest')),
'product_interest_ids' : self._process_many2may(request.httprequest.form.getlist('products_interest')),
'topic_interest_ids' : self._process_many2may(request.httprequest.form.getlist('topics_interest')),
})