Iam trying to add a field "attachment" into the Signup form of Odoo 12. I share with you my work if you can correct me please :
In my file_one.xml :
<template id="auth_signup_fields_extend" inherit_id="auth_signup.fields" name="Signup Fields Extend">
<xpath expr="//div[hasclass('field-confirm_password')]" position="after">
<div class="form-group field-attachment">
<label for="attachment" class="control-label">Attachment</label>
<input type="file" name="attachment" multiple="true" data-show-upload="true" data-show-
caption="true" id="project.id" data-show-preview="true" class="form-control" required="required" t-
att-readonly="'readonly' if only_passwords else None" t-att-autofocus="'autofocus' if login and not
only_passwords else None"/>
</div>
</xpath>
</template>
In my file_2.xml :
<data>
<record id="res_partner_form_inherit" model="ir.ui.view">
<field name="name">res.partner.form.inherit</field>
<field name="model">res.partner</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='function']" position="after">
<field name="attachment"/>
</xpath>
</field>
</record>
</data>
In my model.py
# -*- coding: utf-8 -*-
from odoo import fields, models
class ResPartner(models.Model):
_inherit = "res.partner"
attachment = fields.Binary(string='File of User',attachment=True)
In my file controller.py :
@http.route('/web/signup', type='http', auth='public', website=True,
sitemap=False)
def web_auth_signup(self,**post):
qcontext = self.get_auth_signup_qcontext()
values = {}
if post.get('attachment',False):
Attachments = request.env['ir.attachment']
name = post.get('attachment').filename
file = post.get('attachment')
project_id = post.get('project_id')
attachment = file.read()
attachment_id = Attachments.sudo().create({
'name':name,
'datas_fname': name,
'res_name': name,
'type': 'binary',
'res_model': 'model.model',
'datas': attachment.encode('base64'),
})
value = {
'attachment' : attachment_id
}
return request.render("fl_auth_signup.users", value)
I saw many tutorials and questions/answers about that but my module doesn't work, for now, i have the error : local variable 'Attachments' referenced before assignment
Please, help, can you correct me? ...
You will get the following error:
local variable 'attachment_id' referenced before assignment , line 52, in web_auth_signup
This error will occurs because the following code should be inside if
statement:
value = {'attachment': attachment_id}
return request.render("fl_auth_signup.users", value)
You also used *args
and **kw
(at line 75
) but they are not declared:
return super(AuthSignupHome, self).web_login(*args, **kw)
I checked the original code in auth_signup
and I found that you changed the signature of the web_auth_signup
method. I suggest you keep the signature as it is and use kw
instead of post
.
@http.route('/web/signup', type='http', auth='public', website=True,
sitemap=False)
def web_auth_signup(self, *args, **kw):
qcontext = self.get_auth_signup_qcontext()
# values = {}
if kw.get('attachment', False):
attachments = request.env['ir.attachment']
name = kw.get('attachment').filename
file = kw.get('attachment')
# project_id = kw.get('project_id')
attachment = file.read()
attachment_id = attachments.sudo().create({
'name': name,
'datas_fname': name,
'res_name': name,
'type': 'binary',
'res_model': 'model.model',
'datas': base64.b64encode(attachment),
})
value = {
'attachment': attachment_id
}
return request.render("fl_auth_signup.users", value)
# The rest of your code with no changes
Update the form
tag and set enctype
to multipart/form-data
, it is required when you are using forms that have a file upload control.
<template id="new_signup" inherit_id="auth_signup.signup" name="New Sign up login">
<xpath expr="//form" position="attributes">
<attribute name="enctype">multipart/form-data</attribute>
</xpath>
</template>