Django把现在时间写入数据库,模板渲染在页面中


1. 导入time模块

import time

2. 获取现在时间,使用"年-月-日 时:分:秒"这样的模板,赋值给变量

在views.py中:

pt = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))

3.写入数据库

details.objects.create(time=pt)

4.在页面上渲染

在views.py中:

return render(request,'index.html',{'details':details})

在index.html中:

{% for i in details %}


{{ i.id }}
{{ i.response_partment }}
{{ i.project_manager }}
{{ i.client_name }}
{{ i.price }}
{{ i.time|date:"Y-m-d H:i:s" }}//"年-月-日 时:分:秒"



{{ i.time|date:"Y-m-d" }}//"年-月-日"
{{ i.status }}


{% endfor %}