Search code examples
emailurlodoo-12

Not Getting Full URL in own custom template in odoo?


I am new to odoo and I am trying to get full URL in my own custom template. Please check my code and help to sort it out what I am doing wrong.

mail_template_view.xml

<?xml version="1.0" encoding="utf-8"?>

<data noupdate = "0">
    <record id="example_email_template" model="mail.template">
      <field name="name">Example e-mail template</field>
      <field name="email_from">bhuwankhadka2052@greenit.com.np</field>
      <field name="subject">Congratz khadka</field>
      <field name="email_to">benkhadka143@gmail.com</field>
      <field name="model_id" ref="model_tender_manage"/>
      <field name="auto_delete" eval="True"/>
      <field name="body_html">
          <![CDATA[
            <p>Dear ,<br/><br/>

            Good job, you've just created your first e-mail template!<br/></p>
                My name is Bhuwan Khadka. What about you man?
               A new tender record has been created

                <div>
                <p>Please check the link below for more details:</p>  

                    % set website_url = object.env['ir.config_parameter'].sudo().get_param('web.base.url')
                    Your Odoo domain is: <b><a href="${website_url}">${website_url}</a></b><br>

                </div>

                Regards,<br/>
                Bhuwan Khadka
          ]]>
      </field>
   </record> 
</data>

tendermanage.py

@api.model
def create(self,vals):
    res = super(TenderManage,self).create(vals)
    self.task_send_mail()
    return res

Above code create new record and trigger automate email function

@api.multi
def task_send_mail(self):
    template_email = self.env["mail.template"].search([('name','=','Example e-mail template')]).id
    self.env["mail.template"].browse(template_email).sudo().send_mail(self.id, force_send=True)

Above code send email

If I remove URL code from XML file, email is sent automatically without any error but when URL code is written in email-template I get following error.

Error Message:

enter image description here

Please help me with your suggestion and how to get full url in email template. In XML odoo element is removed as it is not shown in above code.


Solution

  • mail_template_view.xml

    <div>
       <p>Please check the link below for more details:
          Your Odoo domain is: <b><a href="${object.get_full_url()}">Click Here</a></p>
    </div>
    

    tendermanage.py

    @api.multi
    def get_full_url(self):
        self.ensure_one()
        base_url = self.env["ir.config_parameter"].get_param("web.base.url")
        url_params = {
            'id': self.id,
            'view_type': 'form',
            'model': 'your_model',
            'menu_id': self.env.ref('module_name.menu_record_id').id,
            'action': self.env.ref('module_name.action_record_id').id,
        }
        params = '/web?#%s' % url_encode(url_params)
        return base_url + params
    

    Change the mail template div and create method like this in your model.. i hope it will helps..