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

Mysql 严格模式 settings.py

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'"
        }
    }
}

模型日期类型

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)

模板日期格式化

{% 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 %}