这是我的创建方法
def create(cls, data):
try:
cls.format_date_only_element(data,'start_date')
cls.format_date_only_element(data,'end_date')
promotion = cls(**data)
promotion.save()
return True
except:
return False
在上面的代码formate_date_only_element
方法是
_date_only_format = '%Y-%m-%d'
def format_date_only_element(cls,data,date_field):
for r in data:
if date_field in r:
pdate = datetime.strptime(data[date_field], cls._date_only_format)
data[date_field]=pdate
break
我的创建处理程序是
def post(self):
self__uploads =无
form = forms.CreatePromotion(self.request.POST)
count = Promotion.all()。count()
如果count == 0:
计数= 1
form.display_order.choices = [(item + 1,item + 1)for range in items(count)]
image = self.get_uploads('image')
video = self.get_uploads('video')
如果form.validate():
data = form.data
log.info(数据)
upload_url = blobstore.create_upload_url('/ admin / promotion / create')
尝试:
datetime.strptime(data ['start_date'],'%Y-%m-%d')
datetime.strptime(data ['end_date'],'%Y-%m-%d')
除了ValueError:
self.session.add_flash(“日期格式不正确,应为YYYY-MM-DD”,key ='error')
return self.render_response(“/ admin / promotion / create.html”,form = form,upload_url = upload_url)
supp_code=data['supplier_code']
supplier_obj=SupplierMaster.get_by_code(supp_code)
supp_code1=data['supplier_code']
supplier_obj1=Supplier.get_by_code(supp_code1)
if not supplier_obj and supplier_obj1 is None:
self.session.add_flash('Supplier code does not exist',key='error')
return self.render_response("/admin/promotion/create.html", form=form, upload_url=upload_url)
else:
is_video = bool(video)
is_image = bool(image)
if not is_image and not is_video:
self.session.add_flash("Please select a video or image",key='error')
return self.render_response("/admin/promotion/create.html", form=form, upload_url=upload_url)
else:
del data['video']
del data['image']
if is_video:
if video[0].content_type.startswith('video'):
log.info("is video")
data['video'] = video[0]
else:
log.info("is_video")
self.session.add_flash("Please select a video or image",key='error')
return self.redirect("/admin/promotion/create")
if is_image:
if image[0].content_type.startswith('image'):
log.info("is image")
data['image'] = image[0]
else:
log.info("is_image")
self.session.add_flash("Please select a video or image",key='error')
return self.redirect("/admin/promotion/create")
flag = Promotion.create(data)
if flag is True:
self.session.add_flash('Promotion created successfully!',key='success')
else:
self.session.add_flash("Error occured in creating Promotion",key='error')
return self.redirect("/admin/promotions")
else:
upload_url = blobstore.create_upload_url('/admin/promotion/create')
return self.render_response("/admin/promotion/create.html", form=form, upload_url=upload_url)
它给出的输出像(2015-10-29 00:00:00),但我希望我的输出只有日期而不是日期时间,提前谢谢
答案 0 :(得分:0)
您可以在datetime对象上使用.date()
来获取日期 -
pdate = datetime.strptime(data[date_field], cls._date_only_format).date()