python 3 环境安装 与python2 各个版本的却别 django 2 和Django1 的区别

python2 django==1.4.22

reverse导入 from django.core.urlresolvers import reverse
url 直接使用 正则 路径不使用url 函数
导包不同 可以同级导包:from widgets import UEditorWidget
configparser Python2.x 中名为 ConfigParser
django1 事务处理transaction.commit_on_success()
python 2 有xrange range
python2 有from django.utils.encoding import force_unicode
python2 抛出异常 except Exception,e 或者as e
python2 commands
python2 有unicode 和非unicode
长整型 有L
has_key 字典中是否包含指定的key
isinstance 2 可以判断unicode
解压bytearray 类型 prodesc = zlib.decompress(str(prodesc)) # 解压
Django 导包1.4.22from django.utils.importlib import import_module
django 链接方式 from django.db import close_connectionclose_connection()
not isinstance(label_name, (int, basestring)
re.findall(ur’12’, ‘123’)
导包from django.shortcuts import RequestContext
import urllid3
python 2 urllib2、urlparse、和robotparser urlparse.urljoin
Python2 quote 使用 urllib.quote()
python2 urllib2.urlopen()
python2 unicode() 函数 python 3 没有了
python 2 aes 加密没有加密参数 aes = AES.new(key)
python2 有 basestring
python 2 join 导包 from os.path import join
python 2 LANGUAGE_CODE = ‘zh-CN’

python3 django==2.1

from django.urls import reverse
普通路径 用path 正则用 re_path
必须带上上一级的包from DjangoUeditor.widgets import UEditorWidget 对导包路径要求严格
configparser Python2.x 中名为 ConfigParser
django2 处理事务 不知道,没有transaction.commit_on_success(
python3 只有range 没有xrange
python3 没有from django.utils.encoding import force_unicode
只有as e 抛出异常没有 ,e 这样的语法
python3 commands 被 subprocess 取代
python3 只有unicode
没后long
不再支持 需要使用in
3 isinstance 不用判断 unicode
python3 解压 prodesc = zlib.decompress(prodesc) # 解压print (type(prodesc))aa = prodesc.decode(‘utf-8’)weijin_prodesc1 = weijin_prodesc1.encode(‘utf-8’)weijin_prodesc2 = zlib.compress(weijin_prodesc1) # 压缩
django 2 from django.utils.module_loading import import_module 或者 from importlib import import_module
from django.db import close_old_connectionsclose_old_connections()
if not isinstance(label_name, (int, str)
re.findall(r’12’, ‘123’)
from django.template import RequestContext
python3 没有urllid3 替换方法 import urllib3 或者 import urllib.request as urllib2
python3 urllib2、urlparse、和robotparser并入了urllib urllib.error:ContentTooShortError、HTTPError、URLErrorurllib.parse:parseqs、parseqsl、quote、quotefrombytes、quote_plus、unquote unquoteplus、unquoteto_bytes、urldefrag、 urlencode、urljoin、 urlparse、 urlsplit、 urlunparse、 urlunspliturllib.request:AbstractBasicAuthHandler、 AbstractDigestAuthHandler、 BaseHandler、 CatheFTPHandler、 FTPHandler、 FancyURLopener、FileHandler、HTTPBasicAuthHandler、 HTTPCookieProcessor、HTTPDefaultErrorHandler、 HTTPDigestAuthHandler、 HTTPErrorProcessorl、 HTTPHandler、HTTPPasswordMgr、 HTTPPasswordMgrWithDefaultRealm、 HTTPRedirectHandler、HTTPSHandler、OpenerDirector、ProxyBasicAuthHandler ProxyDigestAuthHandler、 ProxyHandler、 Request、URLopener、UnknowHandler、 buildopener、 getproxies、 installopener、 pathname2url、 url2pathname、 urlcleanup、 urlopen、 urlretrieveurllib.response:addbase、addclosehook、addinfo、addinfourlurllib.robotparser:RobotFileParser from urllib.parse import urljoin
python3 from urllib.parse import quote
python3 from urllib.request import urlopen
unicode 已经命名为str()
python3 需要安装包 pip3 install pycryptodome 初始化加密参数 aes = AES.new( key.encode(‘utf-8’),AES.MODE_CBC)
Python3 没有 basestring 用str 代替了 basestring
python3 import os 方式 os.path.join()
python3 LANGUAGE_CODE = ‘zh-Hans’
django.db 没有 metaclass = models.SubfieldBase
urls.py 没有 re_path(r’^static/(?P.*)$’, ‘django.views.static.serve’, {‘document_root’: settings.STATICFILES_DIRS[0]}),
django 2 以上models 的路径必须是相对路径,不能是绝对路径
django2 没有IPAddressField 这个models 改为 GenericIPAddressField
middleware 配置和django 1 不一样
template模板配置不一样

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章