【问题标题】:How to Upload image in Website odoo?如何在网站 odoo 中上传图片?
【发布时间】:2016-02-06 11:17:45
【问题描述】:

我正在开发 Hr_Recruitment 模块。我为 HR->Application 添加了一个二进制图像字段。我正在尝试为外部用户添加功能,以通过网站自己填写工作申请。我添加了姓名、电子邮件、电话,恢复网站中的附件字段以进行工作申请。当他们点击提交时,它在 HR-> 工作申请表中更新。但图像字段在应用程序中没有更新。打开工作申请时它显示类似“不能显示选择的图像”。如何解决这个问题?

控制器/main.py

if post.get('image',False):
            image = request.registry['ir.attachment']
            name = post.get('image').filename      
            file = post.get('image')
                attach = file.stream
                file.show()

                f = attach.getvalue()

                webbrowser.open(image)
            attachment_id = Attachments.create(request.cr, request.uid, {
                    'name': image,
                    'res_name': image,
                    'res_model': 'hr.applicant',
                    'res_id': applicant_id,
                        'datas': base64.decodestring(str(res[0])),
                        'datas_fname': post['image'].filename,
        }, request.context)

views/templates.xml

<div t-attf-class="form-group">
                    <label class="col-md-3 col-sm-4 control-label" for="image">Image</label>
                    <div class="col-md-7 col-sm-8">
                                        <img id="uploadPreview" style="width: 100px; height: 100px;" />
                            <input id="uploadImage" name="image" type="file" class="file" multiple="true" data-show-upload="true" data-show-caption="true" data-show-preview="true" onchange="PreviewImage();"/>
                    </div>
                     </div>

【问题讨论】:

    标签: openerp odoo-8 openerp-8 odoo-9 odoo-website


    【解决方案1】:

    在您的 XML 模板中添加如下所示的图像字段:

    <img itemprop="image" style="margin-top: -53px; margin-left:19px; width:80px;" class="img img-responsive" t-att-src="website.image_url(partner, 'image', None if product_image_big else '300x300')"/>
    <input class="input-file profileChooser" id="fileInput" type="file" name="ufile" onchange="validateProfileImg();"/>
    

    为你删除不必要的属性。

    在您的控制器函数中,您可以从图像字段中获取值:

    vals = {}
    if post['ufile']:
        vals.update({'image': base64.encodestring(post['ufile'].read())})
    request.registry['res.partner'].write(cr, uid, [partner_id], vals)
    

    上面的代码对我有用,我一直在用它来更新 ODOO 网站上的合作伙伴图片。

    【讨论】:

    • 我试过这个 Gopakumar,它似乎不起作用。
    • 你有没有得到任何错误?您使用的是哪个 Odoo 版本?
    【解决方案2】:

    我正在使用 odoo13,我使用这种格式将图像从网站上传到 respartner 表单视图:

    <div class="col-md-6">
       <label for="image_1920" class="form-label">Passport Photo*</label>
       <input type="file" class="form-control" id="image_1920" name="image_1920" required="1" />
    </div>
    
    
    import base64
    
        @http.route('/registered', auth='public', methods=['GET', 'POST'], website=True)
        def registration_submit(self, *args, **kw):
            image = kw.get('image_1920', False)
    
            kw.update({
                'free_member': True,
                'image_1920': base64.encodestring(image.read()) if image else False
            })
            member = request.env['res.partner'].sudo().create(kw)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-19
      • 2020-11-08
      • 2021-08-14
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多