Search code examples
pythonxmlpdfdownloadodoo-12

View or download pdf file on my website Odoo


I want to display an attachment in a page in my website I create a Many2many ('ir.attachment') field in my backend and I want to display a button to download or to display it

it's my code fields

class Contact_us(models.Model):
    _name = "contact.contact_us"
    _description = "contact_us Pages" 

    name = fields.Char(string='Name') 
    description = fields.Html(string='Content')
    attachement = fields.Binary('attachement')
    attachment_ids = fields.Many2many('ir.attachment', 'car_rent_checklist_ir_attachments_rel',
                                  'rental_id', 'attachment_id', string="attachement ids")

and in the other hand is the XML code for the display in my website

        <template id="about_us" name="about_us">
            <t t-call="website.layout">
                <t t-set="additional_title" t-value="about_us.name" />
                    <div id="main-page">

                        <div class="container">
                            <p class="text-right">
                                <h2 style="text-align: center;"><span style="font-size:16px;"><img t-attf-src="/website/image/maarefa.about_us/#{about_us.id}/imagePage/" style="" /></span></h2>
                                <h2 style="text-align: center;"><span style="font-size:18px;"><span style="color:#82661f;"><strong><t t-esc="about_us.name"/></strong></span></span></h2>
                                <div style="text-align: justify;font-size:16px;">
                                    <t t-raw="about_us.description" />

                                </div>


                            </p>

                        </div>

                </div>
            </t>
            <div id="main-page">
            <div class="container">
            <p>
            <t t-if="about_us.attachment_ids">
                    <input t-att-value="about_us.attachment_ids" autocomplete="false" name="attachment_ids" t-att-id="'attachment_ids'+str(about_us.id)" class="youtube_link"
                                readonly="readonly" type="hidden"/>
                 <span class="embed-responsive embed-responsive-16by9" t-att-id="'iframe'+str(about_us.id)"></span>
             </t>
 <t t-foreach="about_us" t-as="f">
    <tr>
        <td><t t-esc="f.attachment_ids"/></td>
        <td><a t-attf-href="/web/binary/saveas?model=maarefa.about_us&amp;field=f.attachement&amp;filename_field=f.attachement_filename&amp;id={{ f.id }}">Download2</a></td>
    </tr>
</t>
</p>

    </div>
    </div>
        </template>

still he does not display anything for attachement i want something like picture

enter image description here


Solution

  • i find a solution for my problem for download a attachment in website i add in my controller py :

    @http.route(['/page/my_page/<model("my.model"):file>'], type='http', auth="public", website=True)
    def my_controller(self, about, **kwargs): 
        file = http.request.env['ir.attachment'].sudo().search([('res_model','=','my.model'),('res_id','=',file.id)])
        print('file:--------------------->>>>>>>>>>',file) 
        values = { 
            'files': file,
        } 
        return request.render("my_module.id_template", values)
    

    and my view in xml

    <t t-foreach="files" t-as="f">
             <div class="col-sm-4">
    
                        <a t-att-href="'/web/content/%i?download=true' % f.id">
                        <img src="/my_module/static/images/icons/backattach.png" style="width:82px; height:86px"  />
                        <span t-esc="f.name" />
                        </a>
    
             </div> 
    </t> 
    

    i find this module link