目标:
- python 2.7.5 -> 3.5.3 (执行更快,更多新功能。工作包括程序语法检查、修改)
- django 1.6 -> 1.9 (功能更加完善、后台更加强大)
- bootstrap 2.3 -> 4.0 (前台更加漂亮、灵活、丰富)
出现问题:
1.[Django]Django Python3出现Error loading MySQLdb module:No module named ‘MySQLdb’问题
vim /apps/dbdat/envdolphin/lib/python3.5/site-packages/django/db/backends/mysql/base.py import pymysql pymysql.install_as_MySQLdb()
2.'WSGIRequest' object has no attribute 'REQUEST'
return int(self.POST['page']) Replace `request.REQUEST` with `POST` and `GET`
3.python3 TypeError: POST data should be bytes or an iterable of bytes. It cannot be of type str
data = urllib.parse.urlencode(d).encode("utf-8") 其中urllib2.urlopen()变成了urllib.request.urlopen() urllib2.Request()变成了urllib.request.Request() website_string = website_bytes_utf8.decode() # to str website.encode(encoding="utf-8") # to bit
4.
import requests r = requests.post("http://example.com", data="Celebrate") ... File "../lib/python3.4/http/client.py", line 1127, in _send_request body = body.encode('iso-8859-1') UnicodeEncodeError: 'latin-1' codec can't encode characters in position 14-15: ordinal not in range(256)
处理方法
data.encode('utf-8') # requests.post 的post 参数
5. Related name must be a valid Python identifier or end with a '+':
rm /apps/dbdat/dolphin/desktop/apps/account/migrations/0001_initial.py
6. 数据库字段变更
Django 1.6 之前使用auth , python manage.py syncdb 生效
1.7 之后,已经整合到框架,生效方法:
add field: python manage.py makemigrations auto python manage.py migrate --fake auto vim auto/models.py python manage.py makemigrations auto python manage.py migrate auto
7. python 代码转换 使用Tools/Scripts/2to3.py
如:
python /apps/dbdat/dolphin/test/pyars-1.8.2/Tools/Scripts/2to3.py -w /apps/dbdat/dolphin/desktop/apps/auto/vco.py
参考资料:
安装包: pip install -r requirements.txt
cat requirements.txt
django==1.9.6
django-celery
django-jsonfield
django-auth-ldap
django-pagination
python-magic
#South
simplejson
#pil
pexpect
django-guardian
celery
pymysql
python3-ldap
ansible
#supervisor
threadpool
uwsgi
bs4
requests
django-utils
readline
Pillow