我正在尝试发送传真,方法是使用函数发送邮件send_mail
@staff_member_required
@require_http_methods(['POST'])
def fax_contract(request, pk=None):
if request.is_ajax() and pk:
print("Sending contract for request {}".format(pk))
try:
contract = Contract.objects.get(pk=pk)
except Contract.DoesNotExist:
return HttpResponseNotFound(_('Contract not found'))
now = datetime.datetime.now()
last_faxed = contract.request.last_faxed_at
if last_faxed and (now - last_faxed) < settings.LOANWOLF_FAX_GRACE_TIME:
return JsonResponse({
'error': True,
'reload': False,
'message': _('Please wait at least %(minutes)d minutes to resend the contracts') % {
'minutes': settings.LOANWOLF_FAX_GRACE_TIME.seconds // 60},
})
else:
contract.request.last_faxed_at = datetime.datetime.now()
contract.request.save()
subject, msg = ('', '')
try:
send_mail = send_mail(subject, msg,
settings.LOANWOLF_FAX_EMAIL_FROM,
settings.LOANWOLF_FAX_EMAIL_TO.format(contract.request.customerprofile.fax),
fail_silently=False)
return send_mail, JsonResponse({'success': True, 'reload': True})
except Exception as e:
return JsonResponse({'error': True, 'message': str(e)})
这是html代码,其中我在哪里以前的方法:
<div class="alert top white-text {{ object.state|request_state_color }}">
<i class="material-icons">info</i>
<a href="{% url "contracts:fax" pk=object.contract.pk %}" class="btn pull-right {{ object.state|request_state_color }} darken-2 btn-process-request" data-turbolinks="false">{%trans "Fax contract" %}</a>
{% if object.contract.pk %}
<a href="{% url "contracts:as-pdf" pk=object.contract.pk %}" class="btn pull-right {{ object.state|request_state_color }} darken-2 workflow-bar-btn-spacer" data-turbolinks="false">{%trans "View contract" %}</a>
{% endif %}
<strong>{% trans "Once the signature is added to the request documents and approved, the deposit will be scheduled." %}</strong>
</div>
这是我到目前为止添加的错误:
error| E0602 local variable 'send_mail' (defined in enclosing scope on line 17) referenced before assignment [pyflakes]
我该如何解决这样的问题?
提前感谢!
附言也许这个问题不清楚。如果是这种情况,请告诉我。
以下是btn-process-request
定义的类:
$('.btn-process-request', node).bind('click', function(e){
e.preventDefault();
var data = {};
if ($(this).data('reason')) {
data.reason = prompt($(this).data('reason'));
if (!data.reason.length && $(this).data('reason-required')) {
alert($(this).data('reason-required'));
return false;
}
}
$.ajax({
url : $(this).attr('href'),
type: 'POST',
data : data,
success: function(data, textStatus, jqXHR) {
if (data.success) {
if (data.redirect_to) {
window.location.href = data.redirect_to;
}
else if (data.reload) {
window.location.reload();
}
}
else {
alert('Error! See console for details :(');
console.error(textStatus, data);
}
},
error: function (jqXHR, textStatus, errorThrown) {
console.error(textStatus, errorThrown);
}
});
return false;
});
使用变量send_mail
来存储从函数外部导入的函数send_mail
的结果。
您可以通过使用不同的变量名称来存储结果来防止警告,例如
result = send_mail(...)
您可能根本不需要存储结果,因为您已使用 fail_silently = False
调用了send_email
。您可以将该行更改为:
send_mail(...)
一旦你解决了这个问题,return 语句就会给你带来问题。
return send_mail, JsonResponse({'success': True, 'reload': True})
一个 Django 视图应该返回一个响应。如果您确实需要结果,请将其包含在响应中。
return JsonResponse({'success': True, 'reload': True, 'result': result})
最后,在 Python 中通常不鼓励使用 except Exception
。您应该尝试捕获更具体的错误。