我的模板中有一个预订表格,它会在提交后发送一封电子邮件。在我的数据库中,日期时间字段显示如下:Oct。 6, 2015, 3:58 p.m. 但是当我收到电子邮件时,日期时间字段显示如下: 2015-10-06 15:58:50.954102 我如何格式化它以便在它显示的电子邮件与它在数据库中的显示方式完全一样?
模型.py
class Booking(models.Model):
patient_name = models.CharField(max_length=1300)
phone = models.IntegerField(null=True, blank = True)
preference = models.CharField(max_length=150,null = True, blank = True) #morning,noon,night
doctor = models.ForeignKey(Doctor)
clinic = models.ForeignKey(Clinic,null=True, blank = True)
datetime = models.DateTimeField(auto_now=True, auto_now_add=True, blank = True, null = True)
def __unicode__(self):
return u"%s %s" % (self.patient_name, self.doctor)
View .py
lead = Booking(doctor_id=doctor.id, clinic_id=doctor.clinic.id, preference=preference, patient_name=patient_name, phone=phone)
lead.save()
body = "Request Made: " + str(lead.datetime) +" "
email = EmailMessage('Blah', body, to=[clinic.email])
email.send()
请您参考如下方法:
您可以使用 strftime 格式化日期字符串
>>> from datetime import date
>>> dt = date(2015, 10, 6, 15, 58, 50)
>>> dt.strftime("%b. %-d %Y %-I:%M %p")
'Oct. 6 2015 2:12 PM'
strftime 的代码列表位于 http://strftime.org/。
所以在你看来你会做类似的事情
body = "Request Made: %s " % lead.datetime.strftime("%b. %-d %Y %-I:%M %p")
