Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
3 Odpowiedzi
5067 Widoki


So I have a POST route in my controller that is supposed to send an email.

controller.py:

class Customer(http.Controller):
    @http.route('/api/hello', methods=['POST'], auth='public', type='http', website=True)
    def index(self, **kw):
        message = Mail(
          from_email='x@gmail.com',
          to_emails='x@gmail.com',
          subject='Notification',
          html_content="Working")
    sg = SendGridAPIClient(os.environ.get('SENDGRID_API_KEY'))
    sg.send(message)

And in my view.xml I have a form asking for name, email and that kind of stuff.

view_field.xml:

<?xml version="1.0" encoding="UTF-8"?>
<odoo>

<record model="ir.ui.view" id="customer_field_form">
    <field name="name">Field</field>
    <field name="model">customer.field</field>
    <field name="arch" type="xml">
        <form action="/api/hello" method="POST">
            <sheet>
                <group>
                    <field name="name" placeholder="Full Name"/><br />
                    <field name="email_address" placeholder="Email Address" /><br />
                    <field name="subject" placeholder="Subject"/><br />
                    <field name="message" placeholder="Message" /><br />
                    <button name="submit" type="submit" > Submit </button>
                </group>
            </sheet>
        </form>
    </field>
</record>

</odoo>

Well no matter what I do my route is never called and the email never sent. I tried putting an < input > instead of a button but it doesn't work either. I really don't see what I'm doing wrong.

If I change my method in a GET and launch it with Chrome the email is sent. So I doubt the problem comes from my route. And yes I have my imported my route in the init.py file.


Awatar
Odrzuć
Najlepsza odpowiedź

Ok your goal is to send email using SendGridAPIClient

Odoo has its own javascript framework which simplifies the things. It handles many complexities and provides an easy way to manage CRUD operation on any model and communicate with the server.​

Here <sheet> <field> are not regular HTML element. it's interpreted by rendering engine and generate pure HTML view and Display it on DOM

So if you want to call the server method on button click then there is no need to define a controller.
on click of <button name="send_email" type="object" string="Send Email" /> it will call send_email method on model

==============================
class MyModel(models.Model):
    _name = 'customer.field'

    @api.multi
    def send_email(self):
        print('here you will get all the field value', self.name, self.email_address)
       # send email code here

==============================

replace view like

<form>
    <sheet>
        <group>
                <field name="name" placeholder="Full Name"/> <br />
                <field name="email_address" placeholder="Email Address" /><br />
                <field name="subject" placeholder="Subject"/><br />
                <field name="message" placeholder="Message" /><br />
                <button name="send_email" type="object" > Send Email </button>
        </group>
    </sheet>
</form>


 Here is the example of it https://www.odoo.com/documentation/11.0/howtos/backend.html#launching-wizards

Awatar
Odrzuć
Autor Najlepsza odpowiedź

Hi Jake,

Sorry but it doesn't work. I tried before to put CSRF=False in the params of my route and yet nothing happens (I also tried what you just did, still nothing).

I am still very new to Odoo, can you confirm to me that my view_field.xml is correct? The form is supposed to be here, under the model? I don't use any templating or anything, can an action on a form be called in a regular xml file? Can a form be placed under a field "arch"? That's the only reasons I'm seeing why it doesn't work, when I click on submit my entry is registered in the db but nothing else seems to happen, nothing is called.

I've been asking 5 other software engineers friends and no one can figure it out, I've been stuck for 2 days. 

Awatar
Odrzuć
Najlepsza odpowiedź

Hi John,

Since version 9, as a part of its commitment to security, Odoo has implemented CSRF tokens. In order for a POST method to be called, you will need to add an input into your form similar to the following:

<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>

This CSRF token will be verified before your controller is even called.

For more information on the topic, check out the Web Controllers page in the Odoo Developer Documents. 


Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
0
maj 25
691
1
lip 24
1446
2
gru 20
2942
2
gru 19
5558
0
lut 18
3155