Django 日记 - mysql 严格模式,模型日期自动更新,格式化模板日期

Mysql 严格模式 settings.py

1
2
3
4
5
6
7
8
9
10
11
12
13
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'django',
'USER': 'django',
'PASSWORD': 'django',
'HOST': 10.10.10.1',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
}
}
}

模型日期类型

1
2
3
4
5
6
7
8
9
10
from django.db import models
import django.utils.timezone as timezone


# Create your models here.
class Person(models.Model):
name = models.CharField(max_length=20)
age = models.IntegerField()
created = models.DateTimeField(default=timezone.now)//DateTimeField(auto_now_add=True)也可以
updated = models.DateTimeField(auto_now=True)

模板日期格式化

1
2
3
4
5
{% extends '../base/base.html' %}
{% block content %}
{{ person.id }} : {{ person.name }} : {{ person.age }}:{{ person.created|date:"Y-m-d H:i:s" }}:
{{ person.updated|date:"Y-m-d H:i:s" }}
{% endblock %}