django 学习笔记 安装

说明:学习完慕课网上的课程「Python升级3.6 强力Django+杀手级Xadmin打造在线教育平台」,成功上线后,做的回顾总结  。感兴趣的也可以自己去入手学习一下

 入坑「python」的「django 框架」,「python」版本是 3.6.5,「django」的版本是 2.1.7,「IDE」是「pycharm」付费版(具体的安装请自行 google),使用的数据库是「mysql 8」


1. 新建项目

   在「pycharm」中新建项目,选择侧边的「django」,使用默认配置,就可以了,点击创建 ,创建完成后,在下方的「Terminal」输入 pip list 查看一下

django 学习笔记 安装_第1张图片
项目创建完成,不过需要更新一下「pip」的版本,后面要安装不少依赖包。推荐使用 windows 的「cmd」去执行,pycharm会出错。


2. 安装依赖包,

后台管理程序使用的是「django」的第三方扩展包「xadmin」,由于我们使用的是「django 2」,按装稍微有点麻烦。在虚拟环境的目录下新建一个「requirements.txt」文件,将需要安装的依赖包的信息写入,后面在执行「pip install」的时候可以方便很多。新建后的文件路径「‪F:\django-review\diandain\venv\Scripts\requirements.txt」这个文件在后面的网站部署时也会用到,文件内容如下:

defusedxml==0.5.0
diff-match-patch==20181111
Django==2.1.7
django-crispy-forms==1.7.2
django-formtools==2.1
django-import-export==1.2.0
django-pure-pagination==0.3.0
django-ranged-response==0.2.0
django-reversion==3.0.3
django-simple-captcha==0.5.10
et-xmlfile==1.0.1
future==0.17.1
httplib2==0.9.2
jdcal==1.4
mysql-connector-python==8.0.15
odfpy==1.4.0
openpyxl==2.6.0
Pillow==5.4.1
protobuf==3.6.1
PyMySQL==0.9.3
pytz==2018.9
PyYAML==3.13
six==1.12.0
tablib==0.12.1
unicodecsv==0.14.1
xlrd==1.2.0
xlwt==1.3.0

 你可以将内容复制到你的 txt 文件中。然后在虚拟换环境中执行

django 学习笔记 安装_第2张图片

执行「activate.bat」进入虚拟环境,退出的时候输入「deactivate」


a. 更新 pip 版本

(venv) F:\django-review\diandain\venv\Scripts>python -m pip install --upgrade pip
Collecting pip
  Using cached https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
  Rolling back uninstall of pip
Exception:
Traceback (most recent call last):
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_internal\basecommand.py", line 228, in main
    status = self.run(options, args)
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_internal\commands\install.py", line 335, in run
    use_user_site=options.use_user_site,
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_internal\req\__init__.py", line 49, in install_given_reqs
    **kwargs
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_internal\req\req_install.py", line 748, in install
    use_user_site=use_user_site, pycompile=pycompile,
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_internal\req\req_install.py", line 961, in move_wheel_files
    warn_script_location=warn_script_location,
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_internal\wheel.py", line 431, in move_wheel_files
    generated.extend(maker.make(spec))
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_vendor\distlib\scripts.py", line 403, in make
    self._make_script(entry, filenames, options=options)
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_vendor\distlib\scripts.py", line 307, in _make_script
    self._write_script(scriptnames, shebang, script, filenames, ext)
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_vendor\distlib\scripts.py", line 243, in _write_script
    launcher = self._get_launcher('t')
  File "F:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg\pip\_vendor\distlib\scripts.py", line 382, in _get_launcher
    result = finder(distlib_package).find(name).bytes
AttributeError: 'NoneType' object has no attribute 'bytes'

(venv) F:\django-review\diandain\venv\Scripts>python -m pip install --upgrade pip
F:\django-review\diandain\venv\lib\site-packages\pip\_internal\req\req_install.py:391: UserWarning: Module pip was already imported from F:\django-review\diandain\venv\lib\site-packages\pip\__init__.py, but f:\django-review\diandain\venv\lib\site-packages\pip-10.0.1-py3.6.egg is being added to sys.path
  self.satisfied_by = pkg_resources.get_distribution(str(no_marker))
Collecting pip
  Using cached https://files.pythonhosted.org/packages/d8/f3/413bab4ff08e1fc4828dfc59996d721917df8e8583ea85385d51125dceff/pip-19.0.3-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
      Successfully uninstalled pip-10.0.1
Successfully installed pip-19.0.3

(venv) F:\django-review\diandain\venv\Scripts>

更新过程出现错误,进入「F:\django-review\diandain\venv\Lib\site-packages」 目录中,将「pip-19.0.3.dist-info」文件删除,重新「pip 」的更新命令,就可以完成更新。


b. 执行命令「pip install -r requirements.txt 」完成依赖包的安装

(venv) F:\django-review\diandain\venv\Scripts>pip install -r requirements.txt
Collecting defusedxml==0.5.0 (from -r requirements.txt (line 1))
  Using cached https://files.pythonhosted.org/packages/87/1c/17f3e3935a913dfe2a5ca85fa5ccbef366bfd82eb318b1f75dadbf0affca/defusedxml-0.5.0-py2.py3-none-any.whl
Collecting diff-match-patch==20181111 (from -r requirements.txt (line 2))
  Using cached https://files.pythonhosted.org/packages/f0/2a/5ba07def0e9107d935aba62cf632afbd0f7c723a98af47ccbcab753d2452/diff-match-patch-20181111.tar.gz
Requirement already satisfied: Django==2.1.7 in f:\django-review\diandain\venv\lib\site-packages (from -r requirements.txt (line 3)) (2.1.7)
Collecting django-crispy-forms==1.7.2 (from -r requirements.txt (line 4))
  Using cached https://files.pythonhosted.org/packages/9a/05/6bad05742d185ec2fabfa4deab05cafde286eb3f383fba24b3674340aca2/django_crispy_forms-1.7.2-py2.py3-none-any.whl
Collecting django-formtools==2.1 (from -r requirements.txt (line 5))
  Using cached https://files.pythonhosted.org/packages/97/3f/b8e04c41c028d5cdad651393abea1f686d846c717d8ab5d5ebe2974f711c/django_formtools-2.1-py2.py3-none-any.whl
Collecting django-import-export==1.2.0 (from -r requirements.txt (line 6))
  Using cached https://files.pythonhosted.org/packages/62/7a/ddd9aef718243504e7715bda9bb5a100cfc353be37dc819d9914a7073cba/django_import_export-1.2.0-py2.py3-none-any.whl
Collecting django-pure-pagination==0.3.0 (from -r requirements.txt (line 7))
  Using cached https://files.pythonhosted.org/packages/55/43/50c475f408d3350cec340855970a5ce02ea12f5a53d520315f200b4847a1/django-pure-pagination-0.3.0.tar.gz
Collecting django-ranged-response==0.2.0 (from -r requirements.txt (line 8))
  Using cached https://files.pythonhosted.org/packages/70/e3/9372fcdca8e9c3205e7979528ccd1a14354a9a24d38efff11c1846ff8bf1/django-ranged-response-0.2.0.tar.gz
Collecting django-reversion==3.0.3 (from -r requirements.txt (line 9))
  Using cached https://files.pythonhosted.org/packages/c2/90/8ef58524fa4e059498abd7f59010ecbc299253c3d6702dd893506344d0b7/django_reversion-3.0.3-py2.py3-none-any.whl
Collecting django-simple-captcha==0.5.10 (from -r requirements.txt (line 10))
  Using cached https://files.pythonhosted.org/packages/72/58/5036ffe717eea6d816684fd2c7b09a477cd72186b35d9f4a340bf214d6d5/django-simple-captcha-0.5.10.zip
Collecting et-xmlfile==1.0.1 (from -r requirements.txt (line 11))
  Using cached https://files.pythonhosted.org/packages/22/28/a99c42aea746e18382ad9fb36f64c1c1f04216f41797f2f0fa567da11388/et_xmlfile-1.0.1.tar.gz
Collecting future==0.17.1 (from -r requirements.txt (line 12))
  Using cached https://files.pythonhosted.org/packages/90/52/e20466b85000a181e1e144fd8305caf2cf475e2f9674e797b222f8105f5f/future-0.17.1.tar.gz
Collecting httplib2==0.9.2 (from -r requirements.txt (line 13))
  Using cached https://files.pythonhosted.org/packages/ff/a9/5751cdf17a70ea89f6dde23ceb1705bfb638fd8cee00f845308bf8d26397/httplib2-0.9.2.tar.gz
Collecting jdcal==1.4 (from -r requirements.txt (line 14))
  Using cached https://files.pythonhosted.org/packages/a0/38/dcf83532480f25284f3ef13f8ed63e03c58a65c9d3ba2a6a894ed9497207/jdcal-1.4-py2.py3-none-any.whl
Collecting mysql-connector-python==8.0.15 (from -r requirements.txt (line 15))
  Using cached https://files.pythonhosted.org/packages/d4/d4/aa2d0b8bf8d0188b6c03fc4e730f0d7495b9f61efb87438fe5e7f477198c/mysql_connector_python-8.0.15-py2.py3-none-any.whl
Collecting odfpy==1.4.0 (from -r requirements.txt (line 16))
  Using cached https://files.pythonhosted.org/packages/85/7d/8f6d1f2a4683be362b101c00232b4c3839e4e4a90e0945d8d43ec6aa671d/odfpy-1.4.0.tar.gz
Collecting openpyxl==2.6.0 (from -r requirements.txt (line 17))
  Using cached https://files.pythonhosted.org/packages/41/a8/257a30b5b7ce0e548bc03f5be3d4b262140c1f7a506038da63cd1f4d34ad/openpyxl-2.6.0.tar.gz
Collecting Pillow==5.4.1 (from -r requirements.txt (line 18))
  Using cached https://files.pythonhosted.org/packages/19/05/e7869088bcd791fe63f6d671577d25c4e3d1fecdc21501254621c65cd74b/Pillow-5.4.1-cp36-cp36m-win32.whl
Collecting protobuf==3.6.1 (from -r requirements.txt (line 19))
  Using cached https://files.pythonhosted.org/packages/23/64/07fe09ea35a7c48b31f9afaa11eb9bab3fe2389a5db70df5601c41e63df3/protobuf-3.6.1-cp36-cp36m-win32.whl
Collecting PyMySQL==0.9.3 (from -r requirements.txt (line 20))
  Using cached https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl
Requirement already satisfied: pytz==2018.9 in f:\django-review\diandain\venv\lib\site-packages (from -r requirements.txt (line 21)) (2018.9)
Collecting PyYAML==3.13 (from -r requirements.txt (line 22))
  Using cached https://files.pythonhosted.org/packages/fb/51/0c49c6caafe8d9a27ad9b0ca9f91adda5a5072b9efbbe7585fb97a4c71c4/PyYAML-3.13-cp36-cp36m-win32.whl
Collecting six==1.12.0 (from -r requirements.txt (line 23))
  Using cached https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Collecting tablib==0.12.1 (from -r requirements.txt (line 24))
  Using cached https://files.pythonhosted.org/packages/e4/9f/cba4e1145ca9ec84d9326f7ce38c6b5f37d9be8bc1af1bd8b19c20374095/tablib-0.12.1.tar.gz
Collecting unicodecsv==0.14.1 (from -r requirements.txt (line 25))
  Using cached https://files.pythonhosted.org/packages/6f/a4/691ab63b17505a26096608cc309960b5a6bdf39e4ba1a793d5f9b1a53270/unicodecsv-0.14.1.tar.gz


(venv) F:\django-review\diandain\venv\Scripts>

c. 安装「xadmin」

这里需要说明一下,无法通过 pip 的方式进行安装适配「django 2」版本的「xadmin」,解决方法是使用「 git」进行安装。当然你的电脑上要安装有「git」,「git 」的安装请自行 google。

关于「xadmin」,可以前往「https://github.com/sshwsfc/xadmin」查看文档获取帮助

在命令行中输入如下命名「pip install git+git://github.com/sshwsfc/xadmin.git@django2」,运行结果如下

(venv) F:\django-review\diandain\venv\Scripts>pip install git+git://github.com/sshwsfc/xadmin.git@django2
Collecting git+git://github.com/sshwsfc/xadmin.git@django2
  Cloning git://github.com/sshwsfc/xadmin.git (to revision django2) to c:\users\刘小北\appdata\local\temp\pip-req-build-w4no9p3j
Switched to a new branch 'django2'
Branch 'django2' set up to track remote branch 'django2' from 'origin'.
Requirement already satisfied: setuptools in f:\django-review\diandain\venv\lib\site-packages\setuptools-39.1.0-py3.6.egg (from xadmin==2.0.1) (39.1.0)
Requirement already satisfied: django>=2 in f:\django-review\diandain\venv\lib\site-packages (from xadmin==2.0.1) (2.1.7)
Collecting django-crispy-forms>=1.6.0 (from xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/9a/05/6bad05742d185ec2fabfa4deab05cafde286eb3f383fba24b3674340aca2/django_crispy_forms-1.7.2-py2.py3-none-any.whl
Collecting django-reversion>=2.0.0 (from xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/c2/90/8ef58524fa4e059498abd7f59010ecbc299253c3d6702dd893506344d0b7/django_reversion-3.0.3-py2.py3-none-any.whl
Collecting django-formtools>=1.0 (from xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/97/3f/b8e04c41c028d5cdad651393abea1f686d846c717d8ab5d5ebe2974f711c/django_formtools-2.1-py2.py3-none-any.whl
Collecting django-import-export>=0.5.1 (from xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/62/7a/ddd9aef718243504e7715bda9bb5a100cfc353be37dc819d9914a7073cba/django_import_export-1.2.0-py2.py3-none-any.whl
Collecting httplib2==0.9.2 (from xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/ff/a9/5751cdf17a70ea89f6dde23ceb1705bfb638fd8cee00f845308bf8d26397/httplib2-0.9.2.tar.gz
Collecting future (from xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/90/52/e20466b85000a181e1e144fd8305caf2cf475e2f9674e797b222f8105f5f/future-0.17.1.tar.gz
Collecting six (from xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/73/fb/00a976f728d0d1fecfe898238ce23f502a721c0ac0ecfedb80e0d88c64e9/six-1.12.0-py2.py3-none-any.whl
Requirement already satisfied: pytz in f:\django-review\diandain\venv\lib\site-packages (from django>=2->xadmin==2.0.1) (2018.9)
Collecting tablib (from django-import-export>=0.5.1->xadmin==2.0.1)
  Downloading https://files.pythonhosted.org/packages/7b/c7/cb74031b330cd94f3580926dc707d148b4ba9138449fc9f433cb79e640d8/tablib-0.13.0-py3-none-any.whl (75kB)
    100% |████████████████████████████████| 81kB 97kB/s
Collecting diff-match-patch (from django-import-export>=0.5.1->xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/f0/2a/5ba07def0e9107d935aba62cf632afbd0f7c723a98af47ccbcab753d2452/diff-match-patch-20181111.tar.gz
Collecting openpyxl>=2.4.0 (from tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Downloading https://files.pythonhosted.org/packages/5f/f8/a5d3a4ab669f99154f87ab531192dd84ac79aae62efab662bd2d82a72194/openpyxl-2.6.1.tar.gz (173kB)
    100% |████████████████████████████████| 174kB 110kB/s
Collecting pyyaml (from tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/fb/51/0c49c6caafe8d9a27ad9b0ca9f91adda5a5072b9efbbe7585fb97a4c71c4/PyYAML-3.13-cp36-cp36m-win32.whl
Collecting xlwt (from tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/44/48/def306413b25c3d01753603b1a222a011b8621aed27cd7f89cbc27e6b0f4/xlwt-1.3.0-py2.py3-none-any.whl
Collecting backports.csv (from tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Downloading https://files.pythonhosted.org/packages/8e/26/a6bd68f13e0f38fbb643d6e497fc3462be83a0b6c4d43425c78bb51a7291/backports.csv-1.0.7-py2.py3-none-any.whl
Collecting odfpy (from tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/85/7d/8f6d1f2a4683be362b101c00232b4c3839e4e4a90e0945d8d43ec6aa671d/odfpy-1.4.0.tar.gz
Collecting xlrd (from tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/b0/16/63576a1a001752e34bf8ea62e367997530dc553b689356b9879339cf45a4/xlrd-1.2.0-py2.py3-none-any.whl
Collecting jdcal (from openpyxl>=2.4.0->tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/a0/38/dcf83532480f25284f3ef13f8ed63e03c58a65c9d3ba2a6a894ed9497207/jdcal-1.4-py2.py3-none-any.whl
Collecting et_xmlfile (from openpyxl>=2.4.0->tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/22/28/a99c42aea746e18382ad9fb36f64c1c1f04216f41797f2f0fa567da11388/et_xmlfile-1.0.1.tar.gz
Collecting defusedxml (from odfpy->tablib->django-import-export>=0.5.1->xadmin==2.0.1)
  Using cached https://files.pythonhosted.org/packages/87/1c/17f3e3935a913dfe2a5ca85fa5ccbef366bfd82eb318b1f75dadbf0affca/defusedxml-0.5.0-py2.py3-none-any.whl
Installing collected packages: django-crispy-forms, django-reversion, django-formtools, jdcal, et-xmlfile, openpyxl, pyyaml, xlwt, backports.csv, defusedxml, odfpy, xlrd, tablib, diff-match-patch, django-import-export, httplib2, future, six, xadmin
  Running setup.py install for et-xmlfile ... done
  Running setup.py install for openpyxl ... done
  Running setup.py install for odfpy ... done
  Running setup.py install for diff-match-patch ... done
  Running setup.py install for httplib2 ... done
  Running setup.py install for future ... done
  Running setup.py install for xadmin ... done
Successfully installed backports.csv-1.0.7 defusedxml-0.5.0 diff-match-patch-20181111 django-crispy-forms-1.7.2 django-formtools-2.1 django-import-export-1.2.0 django-reversion-3.0.3 et-xmlfile-1.0.1 future-0.17.1 httplib2-0.9.2 jdcal-1.4 odfpy-1.4.0 openpyxl-2.6.1 pyyaml-3.13 six-1.12.0 tablib-0.13.0 xadmin-2.0.1 xlrd-1.2.0 xlwt-1.3.0

(venv) F:\django-review\diandain\venv\Scripts>

安装的时间可能有点久,要耐心等待一下。


d. 扩展包

网页分页「django-pure-pagination」和验证码「django-simple-captcha」,后面需要用到这两个扩展包

这两个扩展包的使用方法,可以在 github 上搜索查看他们的文档

分别执行 pip 安装即可

(venv) F:\django-review\diandain\venv\Scripts>pip install django-pure-pagination
Collecting django-pure-pagination
  Using cached https://files.pythonhosted.org/packages/55/43/50c475f408d3350cec340855970a5ce02ea12f5a53d520315f200b4847a1/django-pure-pagination-0.3.0.tar.gz
Installing collected packages: django-pure-pagination
  Running setup.py install for django-pure-pagination ... done
Successfully installed django-pure-pagination-0.3.0

(venv) F:\django-review\diandain\venv\Scripts>pip install  django-simple-captcha
Collecting django-simple-captcha
  Using cached https://files.pythonhosted.org/packages/72/58/5036ffe717eea6d816684fd2c7b09a477cd72186b35d9f4a340bf214d6d5/django-simple-captcha-0.5.10.zip
Requirement already satisfied: six>=1.2.0 in f:\django-review\diandain\venv\lib\site-packages (from django-simple-captcha) (1.12.0)
Requirement already satisfied: Django>=1.8 in f:\django-review\diandain\venv\lib\site-packages (from django-simple-captcha) (2.1.7)
Collecting Pillow!=5.1.0,>=2.2.2 (from django-simple-captcha)
  Using cached https://files.pythonhosted.org/packages/19/05/e7869088bcd791fe63f6d671577d25c4e3d1fecdc21501254621c65cd74b/Pillow-5.4.1-cp36-cp36m-win32.whl
Collecting django-ranged-response==0.2.0 (from django-simple-captcha)
  Using cached https://files.pythonhosted.org/packages/70/e3/9372fcdca8e9c3205e7979528ccd1a14354a9a24d38efff11c1846ff8bf1/django-ranged-response-0.2.0.tar.gz
Requirement already satisfied: pytz in f:\django-review\diandain\venv\lib\site-packages (from Django>=1.8->django-simple-captcha) (2018.9)
Installing collected packages: Pillow, django-ranged-response, django-simple-captcha
  Running setup.py install for django-ranged-response ... done
  Running setup.py install for django-simple-captcha ... done
Successfully installed Pillow-5.4.1 django-ranged-response-0.2.0 django-simple-captcha-0.5.10

(venv) F:\django-review\diandain\venv\Scripts>

3. 简单的测试

进入目录 「(venv)F:\django-review\diandain>」,运行命令「python manage.py runserver」

(venv) F:\django-review\diandain>python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
March 11, 2019 - 11:54:28
Django version 2.1.7, using settings 'diandain.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

进入浏览器,在地址栏输入「http://127.0.0.1:8000/」,查看效果。

django 学习笔记 安装_第3张图片

如果看到这个页面,那么我们的安装就算完成了

你可能感兴趣的:(python_django)