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模板配置不一樣

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