前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2017年8月13日

2017年8月13日

作者头像
阿章-python小学生
发布2018-05-18 17:04:20
6320
发布2018-05-18 17:04:20
举报
文章被收录于专栏:python全栈布道师
  1. img标签的alt用于图片不加载的时候显示,是用来做浏览器引擎优化(SEO)的, title是鼠标放上去显示的,用来给用户看的。
  2. django 模板如果需要应用某个变量或模块,可以在一个view里写一个函数,返回 字典值,再通过settings.py的TEMPLATE的context_processors中进行注册即可。
  3. django日志配置。示例
代码语言:javascript
复制
LOGGING = { 'disable_existing_loggers': False, 'version': 1, 'handlers': {     'console_handler': {         # logging handler that outputs log messages to terminal
         'class': 'logging.StreamHandler',         'level': 'DEBUG',  # message level to be written to console
         'formatter': 'simple',
     },     'default_handler': {         'level': 'DEBUG',         'class': 'logging.handlers.RotatingFileHandler',         'filename': 'log/all.log',  # 日志输出文件
         'maxBytes': 1024 * 1024 * 5,  # 文件大小
         'backupCount': 5,  # 备份份数
         "encoding": "utf-8",         'formatter': 'standard',  # 使用哪种formatters日志格式
     },     'error_handler': {         'level': 'ERROR',         'class': 'logging.handlers.RotatingFileHandler',         'filename': 'log/error.log',         'maxBytes': 1024 * 1024 * 5,         "encoding": "utf-8",         'backupCount': 5,         'formatter': 'standard',
     },     'login_logout_handler': {         'level': 'DEBUG',         'class': 'logging.handlers.RotatingFileHandler',         'filename': 'log/login_logout.log',         'maxBytes': 1024 * 1024 * 5,         "encoding": "utf-8",         'backupCount': 5,         'formatter': 'standard',
     }
 }, 'formatters': {     'standard': {         'format': '%(asctime)s [%(threadName)s:%(thread)d] [%(name)s:%(lineno)d] [%(module)s:%(funcName)s] [%(levelname)s]- %(message)s'},     'simple': {         'format': '%(levelname)s %(message)s'},
 }, 'loggers': {     # 所有django应用下的日志使用DEBUG级别
     'django': {         # this sets root level logger to log debug and higher level
         # logs to console. All other loggers inherit settings from
         # root level logger.
         'handlers': ['console_handler', "default_handler", 'error_handler'],         'level': 'DEBUG',         # 'propagate': True,  # this tells logger to send logging message
         # to its parent (will send if set to True)
     },     # 所有django.template下的日志使用INFO级别,并且不向上(django日志)传递,
     # 原因是如果使用DEBUG级别,碰到不存在的变量会在debug里打印错误栈帧
     'django.template': {         'handlers': ['console_handler', "default_handler", 'error_handler'],         'level': 'INFO',         'propagate': False,
     },     # 所有djanog.template下的日志使用DEBGUG级别打印日志
     'chameleon': {         'handlers': ['console_handler', "default_handler", 'error_handler'],         'level': 'DEBUG',
     },     # 登录登出系统专门用login_logout_handeler记录,并且会继续向上(chameleon)传递,记录到完整的里面
     'chameleon.views.views_login_logout': {         'handlers': ['login_logout_handler'],         'level': 'DEBUG',         'propagate': True,
     }
 },
}

首先根据需求定义处理器handlers,模型formatters。loggers里键为模块的名字。 值里有使用的处理器handler,level是本应用的日志级别,propagate表示是否向上传达。 向上传达的意思是django.template如果向上传达,会传达到django模块。 这里遇到了一个django的bug,即当django.template会在日志级别为DEBUG时再render Template 时遇到没有的变量以DEBUG级别打印错误栈帧轨迹(即使判断了{%if xxx%})。所以解决办法是配置一个 django.template模块的日志级别设为INFO就不再打印了,而且要将propagate设为True,防止向上传递。 而chameleon.views.views_login_logout这个模块希望单独通过一个handler来处理,就配置一个就好了,把 propagate设为True这样chameleon.views.views_login_logout会经由向上传递到chameleon里打印到all.log中。 另外如果想把error日志单独打印到error.log中,不需要额外处理,只需要再handlers中加入error_handler这样当 遇到error日志时就会打印到里面。

  1. django管理后台国际化需要设置 LANGUAGE_CODE = 'zh-hans'TIME_ZONE = 'Asia/Shanghai'USE_TZ = False

最后一项USE_TZ默认是True,这样管理后台显示的时间会比数据库里的时间早8个小时。

1.python读取xml,并用jinja2模板解析。

代码语言:javascript
复制
import xml.etree.cElementTree as ETfrom jinja2 import Template
xml_root_cache = Nonedef get_sql_from_xml(xml, tag, **kwargs):
 logger.info(f"sql-info:get   {tag}   from {xml} kwargs: {kwargs}") global xml_root_cache if DEBUG:
     root = ET.ElementTree(file=xml).getroot() else:     if xml_root_cache is None:
         root = ET.ElementTree(file=xml).getroot()
         xml_root_cache = root     else:
         root = xml_root_cache
 t = root.find(tag) if not hasattr(t, "text"):     return ""
 template = Template(t.text)
 sql = template.render(**kwargs) return sql

2.django中执行原生sql

代码语言:javascript
复制
from django.db import connection, transactiondef db_execute_with_trasaction(sql, *param):
 with connection.cursor() as cursor:
     cursor.execute(sql, *param)
     transaction.commit()def db_execute_with_trasaction(sql, *param):
 with connection.cursor() as cursor:
     cursor.execute(sql, *param)
     transaction.commit()def db_select_all(sql, *params):
 with connection.cursor() as cursor:
     cursor.execute(sql, *params)
     col_names = [desc[0] for desc in cursor.description]
     rows = cursor.fetchall()
     row = [dict(zip(col_names, r)) for r in rows]
 logging.info(f"sql-result:  {row}") return rowdef db_select_one(sql, *params):
 with connection.cursor() as cursor:
     cursor.execute(sql, *params)
     col_names = [desc[0] for desc in cursor.description]
     row = cursor.fetchone()     if row:
         row = dict(zip(col_names, row))     else:
         row = None
 logging.info(f"sql-result:  {row}") return row

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2017-08-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 python全栈布道师 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档