Skip to Content
Menu
This question has been flagged
1 Reply
11103 Views

I'm trying to submit form from Qweb. How can I insert form values in database?

Avatar
Discard

Get web form or template value in controller: https://goo.gl/SEHNbA

Odoo Controller: https://goo.gl/gPjPas

hope this will helps

Best Answer

If you are using website templates then the usual HTML form and submit function is used. You need to specify a controller path to process the data and should be a controller with a route to do further process. I can explain you with an example.

I am going to submit a blog comment through odoo. So here I defined a form


<form id="comment" t-attf-action="/blogpost/comment" method="POST">
<div class="media">
<span class="pull-left">
<img class="img img-circle media-object" t-att-src="website.image_url(user_id.partner_id, 'image_small')" style="width: 30px"/>
</span>
<div class="media-body">
<input name="blog_post_id" t-att-value="blog_post.id" type="hidden"/>
<textarea rows="3" name="comment" class="form-control" placeholder="Write a comment..."></textarea>
<button type="submit" class="btn btn-primary mt8">Post</button>
</div>
</div>
</form>

note that the method is POST. You can specify GET also. Here the action should reach "/blogpost/comment", so we need to write a controller to route the same.


@http.route(['/blogpost/comment'], type='http', auth="public", website=True)
def blog_post_comment(self, blog_post_id=0, **post):
cr, uid, context = request.cr, request.uid, request.context
if not request.session.uid:
return login_redirect()
if post.get('comment'):
user = request.registry['res.users'].browse(cr, uid, uid, context=context)
blog_post = request.registry['blog.post']
blog_post.check_access_rights(cr, uid, 'read')
self._blog_post_message(user, blog_post_id, **post)
blog_post = request.registry['blog.post'].browse(cr, uid, int(blog_post_id), context=context)
return werkzeug.utils.redirect("/blog/%s/post/%s#comments" % (slug(blog_post.blog_id), slug(blog_post)))

Here In post contains the input values from the form that is submitted as a key-value pair.




Avatar
Discard
Author

What is the use of registry[] here?

you can use ENV instead of the registry, this is an example form old API. the registry is actually a pool of models.

Related Posts Replies Views Activity
0
Feb 24
438
1
Mar 24
554
0
Jun 19
3106
1
Dec 16
2185
0
Feb 16
3047