django开发环境搭建(参考流程)

来源:互联网 发布:小猪cms是干什么的 编辑:程序博客网 时间:2024/05/21 12:39

参考博客 django开发环境搭建(参考流程)
Ubuntu下面软件的安装解决包的依赖问题
使用工具virtualenv搭建Python虚拟环境,主要希望解决几个问题:

  • 隔离项目之间的第三方包依赖,如A项目依赖django1.2.5,B项目依赖django1.3。
  • 为部署应用提供方便,把开发环境的虚拟环境打包到生产环境即可,不需要在服务器上再折腾一翻。

  • 安装 virtualenvwrapper (virtualenv的命令包装) ,会自动安装 virtualenv

[root@localhost ~]# pip install virtualenvwrapperYou are using pip version 6.0.8, however version 6.1.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.Collecting virtualenvwrapper  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  DEPRECATION: Implicitly allowing locations which are not hosted at a secure origin is deprecated and will require the use of --trusted-host in the future.  Downloading http://pypi.douban.com/packages/py2.py3/v/virtualenvwrapper/virtualenvwrapper-4.5.0-py2.py3-none-any.whlCollecting virtualenv (from virtualenvwrapper)  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  Downloading http://pypi.douban.com/packages/py2.py3/v/virtualenv/virtualenv-12.1.1-py2.py3-none-any.whl (1.6MB)    100% |################################| 1.6MB 579kB/s Collecting virtualenv-clone (from virtualenvwrapper)  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  Downloading http://pypi.douban.com/packages/source/v/virtualenv-clone/virtualenv-clone-0.2.5.tar.gzCollecting stevedore (from virtualenvwrapper)  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  Downloading http://pypi.douban.com/packages/py2.py3/s/stevedore/stevedore-1.3.0-py2.py3-none-any.whlCollecting argparse (from stevedore->virtualenvwrapper)  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  Downloading http://pypi.douban.com/packages/2.7/a/argparse/argparse-1.3.0-py2.py3-none-any.whlCollecting six>=1.9.0 (from stevedore->virtualenvwrapper)  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  Downloading http://pypi.douban.com/packages/3.3/s/six/six-1.9.0-py2.py3-none-any.whlCollecting pbr!=0.7,<1.0,>=0.6 (from stevedore->virtualenvwrapper)  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  Downloading http://pypi.douban.com/packages/py2.py3/p/pbr/pbr-0.10.8-py2.py3-none-any.whl (70kB)    100% |################################| 73kB 317kB/s Requirement already satisfied (use --upgrade to upgrade): pip in /usr/lib/python2.7/site-packages (from pbr!=0.7,<1.0,>=0.6->stevedore->virtualenvwrapper)Installing collected packages: pbr, six, argparse, stevedore, virtualenv-clone, virtualenv, virtualenvwrapper  Running setup.py install for virtualenv-clone    Installing virtualenv-clone script to /usr/binSuccessfully installed argparse-1.3.0 pbr-0.10.8 six-1.9.0 stevedore-1.3.0 virtualenv-12.1.1 virtualenv-clone-0.2.5 virtualenvwrapper-4.5.0You have new mail in /var/spool/mail/rootvirtualenv
  • 修改 ~/.bashrc ,把以下内容加入.bashrc 的尾部
export WORKON_HOME=$HOME/.virtualenvsexport PROJECT_HOME=$HOME/directory-you-do-development-insource /usr/local/bin/virtualenvwrapper.sh
  • 然后执行命令让virtualenvwrapper命令生效
source .bashrc

创建虚拟环境

請於命令列模式下輸入下列指令:

$ virtualenv [指定虛擬環境的名稱]

例如下列指令會建立名為 “ENV” 的虛擬環境:

[root@localhost ~]#  virtualenv ENVNew python executable in ENV/bin/pythonInstalling setuptools, pip...done.[root@localhost ~]# lsanaconda-ks.cfg  distribute_setup.py  ELcloud  ENV  get-pip.py  maven_mahout_template  scipy

預設在建立虛擬環境時,會依賴系統環境中的 site packages,如果想完全不依賴系統的 packages,可以加上參數–no-site-packages 來建立虛擬環境:

[root@localhost ~]# virtualenv --no-site-packages ENVNew python executable in ENV/bin/pythonInstalling setuptools, pip...done.You have new mail in /var/spool/mail/root

啟動虛擬環境

請先切換當前目錄至建立的虛擬環境中。前例中,建立名稱為 “ENV”,則:

[root@localhost ~]# cd ENV

接著,啟動虛擬環境:

[root@localhost ENV]# source bin/activate(ENV)[root@localhost ENV]# 
(ENV)[root@localhost ENV]# exitlogout
[root@localhost ~]#  virtualenv -hUsage: virtualenv [OPTIONS] DEST_DIROptions:  --version             show program's version number and exit  -h, --help            show this help message and exit  -v, --verbose         Increase verbosity.  -q, --quiet           Decrease verbosity.  -p PYTHON_EXE, --python=PYTHON_EXE                        The Python interpreter to use, e.g.,                        --python=python2.5 will use the python2.5 interpreter                        to create the new environment.  The default is the                        interpreter that virtualenv was installed with                        (/usr/bin/python)  --clear               Clear out the non-root install and start from scratch.  --no-site-packages    DEPRECATED. Retained only for backward compatibility.                        Not having access to global site-packages is now the                        default behavior.  --system-site-packages                        Give the virtual environment access to the global                        site-packages.  --always-copy         Always copy files rather than symlinking.  --unzip-setuptools    Unzip Setuptools when installing it.  --relocatable         Make an EXISTING virtualenv environment relocatable.                        This fixes up scripts and makes all .pth files                        relative.  --no-setuptools       Do not install setuptools (or pip) in the new                        virtualenv.  --no-pip              Do not install pip in the new virtualenv.  --extra-search-dir=DIR                        Directory to look for setuptools/pip distributions in.                        This option can be used multiple times.  --never-download      DEPRECATED. Retained only for backward compatibility.                        This option has no effect. Virtualenv never downloads                        pip or setuptools.  --prompt=PROMPT       Provides an alternative prompt prefix for this                        environment.  --setuptools          DEPRECATED. Retained only for backward compatibility.                        This option has no effect.  --distribute          DEPRECATED. Retained only for backward compatibility.                        This option has no effect.

安装django

[root@localhost ~]# pip install djangoYou are using pip version 6.0.8, however version 6.1.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.Requirement already satisfied (use --upgrade to upgrade): django in /usr/lib/python2.7/site-packagesYou have new mail in /var/spool/mail/root

验证安装情况

[root@localhost ~]# which django-admin.py/usr/bin/django-admin.pyYou have new mail in /var/spool/mail/ro

建立一个名称为 ELcloud 的django项目

[root@localhost ~]# django-admin.py startproject ELcloud[root@localhost ~]# lsanaconda-ks.cfg  distribute_setup.py  ELcloud  get-pip.py  maven_mahout_template  scipy

查看ELcloud目录

[root@localhost ~]# ls -R  ELcloud/ ELcloud/:ELcloud  manage.pyELcloud/ELcloud:__init__.py  settings.py  urls.py  wsgi.py

初始化本地 git 库

[root@localhost ~]# cd ~/ELcloudYou have new mail in /var/spool/mail/root[root@localhost ELcloud]#  git initInitialized empty Git repository in /root/ELcloud/.git

通过South对django数据库进行维护

django的syncdb命令在数据库表首次建立时能够根据model建立相应的表,但是一旦数据库中有同名的表,则syncdb命令不会更新数据库。虽然不能说django这样设计不好,但确实挺麻烦。

South解决了这个问题,能够检测对models的更改并同步到数据

[root@localhost ELcloud]# pip install southYou are using pip version 6.0.8, however version 6.1.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.Collecting south  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  DEPRECATION: Implicitly allowing locations which are not hosted at a secure origin is deprecated and will require the use of --trusted-host in the future.  Downloading http://pypi.douban.com/packages/source/S/South/South-1.0.2.tar.gz (96kB)    100% |################################| 98kB 5.8MB/s Installing collected packages: south  Running setup.py install for southSuccessfully installed south-1.0.2

安装django-debug-toobal插件帮助开发调试
主站网址 django debug toolbar是一个django下的插件,能够在浏览器侧面显示一个面板,在实际开发中用来查看变量,分析性能等各方面都有不错的效果。
版本 0.9.4 显示的内容
django版本
cpu运行时间
django的settings
HTTP Headers
Request Vars (view函数、Cookies Variables、Session Variables、Get Variables、Post Variables)
SQL的实际语句及状态
模板及上下文参数
Signals
Logging

[root@localhost ELcloud]# pip install django-debug-toolbarYou are using pip version 6.0.8, however version 6.1.1 is available.You should consider upgrading via the 'pip install --upgrade pip' command.Collecting django-debug-toolbar  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  DEPRECATION: Implicitly allowing locations which are not hosted at a secure origin is deprecated and will require the use of --trusted-host in the future.  Downloading http://pypi.douban.com/packages/2.7/d/django-debug-toolbar/django_debug_toolbar-1.3.0-py2.py3-none-any.whl (203kB)    100% |################################| 204kB 649kB/s Requirement already satisfied (use --upgrade to upgrade): django>=1.4.2 in /usr/lib/python2.7/site-packages (from django-debug-toolbar)Collecting sqlparse (from django-debug-toolbar)  This repository located at pypi.douban.com is not a trusted host, if this repository is available via HTTPS it is recommend to use HTTPS instead, otherwise you may silence this warning with '--trusted-host pypi.douban.com'.  Downloading http://pypi.douban.com/packages/source/s/sqlparse/sqlparse-0.1.15.tar.gz (57kB)    100% |################################| 61kB 2.8MB/s Installing collected packages: sqlparse, django-debug-toolbar  Running setup.py install for sqlparse    changing mode of build/scripts-2.7/sqlformat from 644 to 755    changing mode of /usr/bin/sqlformat to 755Successfully installed django-debug-toolbar-1.3.0 sqlparse-0.1.15

使用fabric进行自动部署
Fabric是一个Python库,可以通过SSH在多个host上批量执行任务。你可以编写任务脚本,然后通过Fabric在本地就可以使用SSH在大量远程服务器上自动运行。这些功能非常适合应用的自动化部署,或者执行系统管理任务。

pip install fabric
0 0
原创粉丝点击