【发布时间】:2021-09-08 12:14:45
【问题描述】:
我在 Django 中使用 SendGrid,我有两个单独的函数。一个使用 SendGrid 代码(见下文 - 工作代码)工作,然后另一个不断收到错误 - 'TypeError' 对象没有属性 'booking_message'。
我想这个问题很小,但我一直想念它。
感谢我能得到的任何帮助。
(注意 - 我已经取出了模板 ID、发件人和收件人电子邮件地址以及 SendGrid API 应用程序 ID,只是让 '')在发布之前我没有时间使用环境变量来隐藏密钥。
工作代码
class ASPBookingsCreateView(CreateView):
form_class = ASPBookingsForm
model = ASPBookings
template_name = "vistours/asp.html"
def post(self, request):
# Save the new booking to the database.
if request.method == 'POST':
form = ASPBookingsForm(request.POST)
if form.is_valid():
print(request.POST)
form.save()
# Get the data for the emails.
asp_data = ASPBookings.objects.all()
asp_data.contact_name = request.POST['contact_name']
asp_data.booking_email = request.POST['email']
asp_data.booking_date = request.POST['booking_date']
asp_data.booking_time = request.POST['booking_time']
asp_data.program_type =request.POST['program_type']
# Format Date and Time
formatted_date = parse_date(asp_data.booking_date).strftime('%A %d %B %Y')
print(formatted_date)
# Add SendGrid Template ID's
BOOKING_TEMPLATE_ID = ''
UPDATE_TEMPLATE_ID = ''
# Send confirmation email of the booking.
booking_message = Mail(from_email='',
to_emails=[asp_data.booking_email],
)
# Add Template ID to booking message.
booking_message.template_id = BOOKING_TEMPLATE_ID
# Add Dynamic Data
booking_message.dynamic_template_data = {
'contact_name': asp_data.contact_name,
'booking_date': formatted_date,
'booking_time': asp_data.booking_time,
}
# Send new booking alert to VIS Staff.
message = Mail(from_email='',
to_emails='')
message.template_id = UPDATE_TEMPLATE_ID
# Add Dynamic Data
message.dynamic_template_data = {
'contact_name': asp_data.contact_name,
'booking_date': formatted_date,
'booking_time': asp_data.booking_time,
'program_type': asp_data.program_type,
}
# Send Email
try:
sg = SendGridAPIClient('')
sg2 = SendGridAPIClient('')
response = sg.send(booking_message)
responses = sg2.send(message)
except Exception as e:
print(e.message)
else:
print(form.errors)
form()
return render(request, 'vistours/success.html')
代码出现类型错误 错误 - “TypeError”对象没有“booking_message”属性。
def confirm_booking(request, id, program_type):
if program_type == 'asp':
confirm_booking = ASPBookings.objects.get(id=id)
confirm_booking.status = 'Confirmed'
confirm_booking.save()
# Format Date and Time
formatted_date = confirm_booking.booking_date.strftime('%A %d %B %Y')
# print(formatted_date)
# Add SendGrid Template ID's
CONFIRMED_BOOKING_TEMPLATE_ID = ''
# Send confirmation email of the booking.
booking_message = Mail(from_email='',
to_emails=[confirm_booking.email])
# Add Template ID to booking message.
booking_message.template_id = CONFIRMED_BOOKING_TEMPLATE_ID
# Add Dynamic Data
booking_message.dynamic_template_data = {
'contact_name': confirm_booking.contact_name,
'booking_date': confirm_booking.booking_date,
'booking_time': confirm_booking.booking_time,
}
# Send Email
try:
sg = SendGridAPIClient('')
response = sg.send(booking_message)
except Exception as e:
print(e.booking_message)
elif program_type == 'bfbw':
confirm_booking = BFBWBookings.objects.get(id=id)
confirm_booking.status = 'Confirmed'
confirm_booking.save()
# # Format Date and Time
# formatted_date = parse_date(confirm_booking.booking_date).strftime('%A %d %B %Y')
# print(formatted_date)
# Add SendGrid Template ID's
BOOKING_TEMPLATE_ID = ''
# Send confirmation email of the booking.
booking_message = Mail(from_email='',
to_emails=[confirm_booking.email])
# Add Template ID to booking message.
booking_message.template_id = BOOKING_TEMPLATE_ID
# Add Dynamic Data
booking_message.dynamic_template_data = {
'contact_name': confirm_booking.contact_name,
'booking_date': confirm_booking.booking_date,
'booking_time': confirm_booking.booking_time,
}
# Send Email
try:
sg = SendGridAPIClient('')
response = sg.send(booking_message)
except Exception as e:
print(e.booking_message)
return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
【问题讨论】:
-
您尝试在您的
except块中打印e.booking_message,错误很可能没有此属性? -
谢谢伊恩,就是这样。一旦我将其更改为 print(e) 就可以了。
-
出于兴趣,我正在尝试格式化日期 confirm_booking.booking_date.strftime('%A %d %B %Y') 但在添加时不断收到消息 Object of type date is not JSON serializable它到 booking_message.dynamic_template_data。你知道如何解决这个问题吗?我知道这超出了原始问题的范围,所以如果我需要重新发布一个新问题,请不要担心。
-
您只在第一个函数 (
post) 的booking_message.dynamic_template_data中设置了formatted_date,而不是第二个函数 (confirm_booking)。这可能是你的问题吗? -
意味着我设法解决了这个问题。感谢 Philnash 的帮助,我将设法关闭此问题。
标签: django django-views typeerror sendgrid