查看 djangobook 中的 django 代码:
from django.http import Http404, HttpResponse
import datetime
def hours_ahead(request, offset):
try:
offset = int(offset)
except ValueError:
raise Http404()
dt = datetime.datetime.now() + datetime.timedelta(hours=offset)
html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt)
return HttpResponse(html)
尝试后,它将偏移量转换为整数,对吗?在'datetime.timedelta(hours=offset)'行中,偏移量用作整数,但在'html = "In %s hour(s)行中,它将是%s。" %(偏移量,dt)'
offset 是一个 %s ,它是一个字符串,对吧?还是我错过了理解?我以为 %s 只能是字符串,不能是整数?