以下是代码和Traceback:
@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},
})
else:
contract.request.last_faxed_at = datetime.datetime.now()
contract.request.save()
subject, msg = ('', '')
# TODO: audit log
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 JsonResponse({'success': True, 'reload': True})
except Exception as e:
return JsonResponse({'error': True, 'message': str(e)})
和
Traceback (most recent call last):
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 170, in __call__
response = self.get_response(request)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 124, in get_response
response = self._middleware_chain(request)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
response = response_for_exception(request, exc)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 86, in response_for_exception
response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 128, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django_extensions/management/technical_response.py", line 6, in null_technical_500_response
six.reraise(exc_type, exc_value, tb)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/exception.py", line 39, in inner
response = get_response(request)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
response = self._get_response(request)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/views/decorators/http.py", line 40, in inner
return func(request, *args, **kwargs)
File "/home/jeremie/Projects/24-django/loanwolf/contracts/views.py", line 71, in fax_contract
'minutes': settings.LOANWOLF_FAX_GRACE_TIME},
File "/home/jeremie/Projects/24-django/venv/lib/python2.7/site-packages/django/utils/functional.py", line 159, in __mod__
return six.text_type(self) % rhs
TypeError: %d format: a number is required, not datetime.timedelta
问题显然位于定义LOANWOLF_FAX_GRACE_TIME
的位置。所以这就是那一行:LOANWOLF_FAX_GRACE_TIME = timedelta(minutes=5)
。
事实上,我有一个按钮,每次按下它,它都会传真到某个地方。我们希望防止有人发送许多传真副本。因此,我们希望在每次发送传真之间插入五分钟的时间间隔。我怎么能在这里解决这个问题。
答案 0 :(得分:1)
Formatter需要一个整数,而你传递timedelta
实例。您可以将其转换为秒,然后将其除以60:
...
'minutes': settings.LOANWOLF_FAX_GRACE_TIME.seconds // 60
})