1.先在settings.py設定
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'simple': {
# exact format is not important, this is the minimum information
'format': '%(asctime)s %(name)-12s %(lineno)d %(levelname)-8s %(message)s',
},
},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
},
# 'mail_admins': { # Add Handler for mail_admins for `warning` and above
# 'level': 'ERROR',
# 'class': 'django.utils.log.AdminEmailHandler',
# },
'file': {
'class': 'logging.FileHandler',
'filename': os.path.join(LOG_DIR, 'admin.log'),
},
},
'root': {
'handlers': ['console', 'file'],
'level': 'INFO',
},
}
2.admin.py
def save_model(self, request, obj, form, change):
if change:
# print('self',self)
# print('request',request)
# print('obj',obj)
# print('form',form)
# print("form.cleaned_data['project_name']",form.cleaned_data['project_name'])
# print('change',change)
logger.info("%s %s modified the data of %s " % (datetime.now(),request.user.username, obj.project_name))
super().save_model(request, obj, form, change)
3.views.py
# dashboard
def dashboard(request):
# 篩選數據
sitedata = request.POST.get('sitedata', '')
statusdata = request.POST.get('statusdata', '')
project_all = project.objects.all()
project_items = project_all
if sitedata:
# 如果sitedata存在
if sitedata!='---':
project_items = project_items & project_all.filter(Q(project_dept=sitedata))
else:
project_items = project_items & project_all
else:
# 如果sitedata不存在
project_items = project_items & project_all
if statusdata:
# 如果statusdata存在
if statusdata!='---':
project_items = project_items & project_all.filter(Q(project_status=statusdata))
else:
project_items = project_items & project_all
else:
# 如果statusdata不存在
project_items = project_items & project_all
# 排序
project_items = project_items.order_by('-project_owner_name','project_plan_start_date')
logger.info("%s %s query %s pcs data" % (datetime.now(),request.user.username, len(project_items)))
return render(request, 'dashboard.html',locals())
Yesterday, Xiaobian received a