在我的网站 Odoo 上查看或下载 pdf 文件

View or download pdf file on my website Odoo

我想在我网站的页面中显示附件我在后端创建了一个 Many2many ('ir.attachment') 字段,我想显示一个按钮来下载或显示它

这是我的代码字段

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")

另一方面是XML我网站显示的代码

        <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>

他仍然没有显示任何附件 我想要类似图片的东西

我找到了解决我的问题的方法,可以在网站上下载附件 我在我的控制器 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)

以及我在 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> 

我找到了这个模块 link