Python-FAQ

Python-FAQ

Python-FAQ

django的model死活不更新

1
2
3
4
5
rm -rf APP_NAME/migrations
select * from django_migrations;
delete * from django_migrations where app='APP_NAME";
python3 manage.py makemigrations APP_NAME
python3 manage.py migrate APP_NAME

utc time str to shanghai time str

1
2
3
4
5
  # prometheus default is utc time, change it to +8 time
  dt1 = datetime.strptime(alert_params['startsAt'], "%Y-%m-%dT%H:%M:%S.%fZ")
  datetime_with_timezone = datetime(dt1.year, dt1.month, dt1.day, dt1.hour, dt1.minute, dt1.second, 0, UTC)
  dt2 = datetime_with_timezone.astimezone(timezone('Asia/Shanghai'))
  start_time_str = dt2.strftime("%Y-%m-%d %H:%M:%S")

豆瓣源

1
-i http://pypi.douban.com/simple --trusted-host pypi.douban.com

django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?

1
2
pipenv install pymysql
pipenv install mysqlclient

No module named 'dateutil'

1
pip3 install python-dateutil

python限制参数类型

使用注解 官方文档

python smtp发送邮件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  # Date: Tue Nov 29 10:59:43 2022
  # Author: liuliancao <liuliancao@gmail.com>
  """Description: Python smtp example."""
  import smtplib

  def send_mail(smtp_host, username, password, subject="test subject", content="test message", smtp_port=465, dst=""):
      """
	 Send mail by smtp.
	 @smtp_host: smtp host like smtp.xxx.com
	 @smtp_port: smtp port like 25 or 465
	 @username: your mail username
	 @password: your mail password
      """
      try:
	  smtp = smtplib.SMTP()
	  smtp.set_debuglevel(1)
	  smtp.connect(smtp_host, smtp_port)
	  smtp.login(username, password)
	  msg = MIMEText(content)
	  smtp.sendmail(msg_from=username, msg_to=dst, msg=msg.as_string())
      except Exception as e:
	  print("smtp connect or login failed ", str(e))
      finally:
	  pass

参考文档