Python 上传注册表格Odoo 12中的文件
我正试图在Odoo 12的注册表格中添加一个字段“附件”。如果您能纠正我,我将与您分享我的工作,请: 在我的文件_one.xml中:Python 上传注册表格Odoo 12中的文件,python,odoo,attachment,sign,odoo-12,Python,Odoo,Attachment,Sign,Odoo 12,我正试图在Odoo 12的注册表格中添加一个字段“附件”。如果您能纠正我,我将与您分享我的工作,请: 在我的文件_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"
<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>
在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)
我看到了很多关于这方面的教程和问题/答案,但我的模块不起作用,现在,我有一个错误:在分配任务之前引用了局部变量“Attachments”
请帮帮我,你能纠正我吗 您将得到以下错误:
local variable 'attachment_id' referenced before assignment , line 52, in web_auth_signup
发生此错误的原因是以下代码应位于if
语句中:
value = {'attachment': attachment_id}
return request.render("fl_auth_signup.users", value)
您还使用了*args
和**kw
(第75行)但未声明:
return super(AuthSignupHome, self).web_login(*args, **kw)
我在auth\u signup
中检查了原始代码,发现您更改了web\u auth\u signup
方法的签名。我建议您保留签名,并使用kw
而不是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
更新表单
标记并将enctype
设置为多部分/表单数据
,这是使用具有文件上载控件的表单时所必需的
<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>
多部分/表单数据
请显示错误日志的最后几行。控制器中不能有此错误。嗨@Kenly,我正在使用Windows10,当我尝试访问以下页面时:localhost:8069/web/signup,我不能,因为我有此错误:“分配前引用了局部变量‘附件’”,请帮助!!!!尝试找出产生此错误的行。打开C:\Program Files(x86)\Odoo 12.0\server\Odoo.log
并查找包含分配前引用的的行。感谢您的解释,即:文件“C:\Program Files(x86)\Odoo 12.0e\server\Odoo\addons\fl\u auth\u signup\controllers\main.py”,第43行,在web\u auth\u signup UnboundLocalError:分配前引用的局部变量“Attachments”请在fl\u auth\u signup\controllers\main.py
中提供web\u auth\u signup
的代码。这个答案对您有帮助吗?我很乐意提供帮助
@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
<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>