我有麻烦将日期从HTML转换成Python字符串,以便在查询中使用它
我想用日期参数向MySQL发送查询。
roomList=Room.objects.raw(
"select room.id "
+"from room join client_room on room.id=client_room.room_id "
+"where client_room.date_in>'"+request.GET.get("date_out")+ ..."#argument-date_out
我从HTML接收日期
<p>
<label>Enter first date</label><br>
<input type="date" name="date_in" value="{{date1}}"/>
</p>
<p>
<label>Enter second date</label><br>
<input type="date" name="date_out" value="{{date2}}"/>
</p>
当单独打印日期时,它可以正确地打印
print(request.GET.get("date_in"))
2021-04-01#Terminal
但如果尝试赋值和/或串联,则返回非类型
date=request.GET.get("date_in")
print("Date is " + date)
can only concatenate str (not "NoneType") to str #HTML Response
尝试使用strftime
:
from datetime import datetime
print("Date is " + datetime.strftime(date))