{"action":"opened","issue":{"url":"https://api.github.com/repos/JeffyLu/JeffyLu.github.io/issues/9","repository_url":"https://api.github.com/repos/JeffyLu/JeffyLu.github.io","labels_url":"https://api.github.com/repos/JeffyLu/JeffyLu.github.io/issues/9/labels{/name}","comments_url":"https://api.github.com/repos/JeffyLu/JeffyLu.github.io/issues/9/comments","events_url":"https://api.github.com/repos/JeffyLu/JeffyLu.github.io/issues/9/events","html_url":"https://github.com/JeffyLu/JeffyLu.github.io/issues/9","id":193682954,"number":9,"title":"PythonAnywhere部署Django----virtualenv虚拟环境","user":{"login":"JeffyLu","id":16357973,"avatar_url":"https://avatars.githubusercontent.com/u/16357973?v=3","gravatar_id":"","url":"https://api.github.com/users/JeffyLu","html_url":"https://github.com/JeffyLu","followers_url":"https://api.github.com/users/JeffyLu/followers","following_url":"https://api.github.com/users/JeffyLu/following{/other_user}","gists_url":"https://api.github.com/users/JeffyLu/gists{/gist_id}","starred_url":"https://api.github.com/users/JeffyLu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JeffyLu/subscriptions","organizations_url":"https://api.github.com/users/JeffyLu/orgs","repos_url":"https://api.github.com/users/JeffyLu/repos","events_url":"https://api.github.com/users/JeffyLu/events{/privacy}","received_events_url":"https://api.github.com/users/JeffyLu/received_events","type":"User","site_admin":false},"labels":[{"id":479624530,"url":"https://api.github.com/repos/JeffyLu/JeffyLu.github.io/labels/Python","name":"Python","color":"c2e0c6","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-12-06T04:52:51Z","updated_at":"2016-12-06T04:52:51Z","closed_at":null,"body":"# PythonAnywhere部署Django----virtualenv虚拟环境\r\n###### 作者：[JeffyLu](https://jeffylu.github.io/)\r\n\r\n- - -\r\n\r\n### 前期准备\r\n- 推送项目到github上\r\n- 注册PythonAnywhere帐号\r\n\r\n### 克隆项目到PythonAnywhere本地\r\n1. 打开控制台\r\n![控制台](https://raw.githubusercontent.com/JeffyLu/JeffyLu.github.io/master/temp_img/python/#9/1.png)\r\n\r\n2. 克隆\r\n```\r\n$ git clone https://github.com/your username/your repository name.git\r\n```\r\n\r\n### 创建virtualenv虚拟环境\r\n1. 在刚刚的控制台中切换路径到克隆下来的仓库里\r\n```\r\n$ cd your repository name\r\n```\r\n\r\n2. 创建虚拟环境，选择python版本\r\n```\r\n$ virtualenv --python=python3.4 myvenv\r\n```\r\n\r\n3. 在虚拟环境中安装django和whitenoise\r\n```\r\n$ source myvenv/bin/activate\r\n(mvenv) $ pip install django whitenoise\r\n```\r\n\r\n### 发布前的一些工作\r\n1. 收集静态文件\r\n```\r\n(mvenv) $ python manage.py collectstatic\r\n```\r\n\r\n2. 迁移数据库\r\n```\r\n(mvenv) $ python manage.py makemigrations\r\n(mvenv) $ python manage.py migrate\r\n```\r\n\r\n3. 创建管理员用户(可选)\r\n```\r\n(mvenv) $ python manage.py createsuperuser\r\n```\r\n\r\n### 新建一个web应用\r\n1. 选择Add a new web app\r\n![add a new app](https://raw.githubusercontent.com/JeffyLu/JeffyLu.github.io/master/temp_img/python/#9/2.png)\r\n\r\n2. 在弹出的对话框中选择最底下的 ”manual configuration“ (***注意:不是选择django***)\r\n3. 选择对应的python版本\r\n\r\n### 配置web应用\r\n1. 在web栏下找到Virtualenv，填写刚刚的虚拟环境位置\r\n![virtualenv](https://raw.githubusercontent.com/JeffyLu/JeffyLu.github.io/master/temp_img/python/#9/3.png)\r\n\r\n2. 找到Code，编辑WSGI configuration file上的wsgi文件，添加如下代码：\r\n![code](https://raw.githubusercontent.com/JeffyLu/JeffyLu.github.io/master/temp_img/python/#9/4.png)\r\n```python\r\nimport os\r\nimport sys\r\npath = '/to/your/project/path'  \r\nif path not in sys.path:\r\n\tsys.path.append(path)\r\nos.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'\r\nfrom django.core.wsgi import get_wsgi_application\r\nfrom whitenoise.django import DjangoWhiteNoise\r\napplication = DjangoWhiteNoise(get_wsgi_application())\r\n```\r\n\r\n3. 完成之后点击最上面大大的绿色Reload按钮，若能成功访问”yourusername.pythonanywhere.com“，那部署就算成功了。    \r\n\r\n* * *\r\n###### 参考：[djangogirls](https://tutorial.djangogirls.org/zh/deploy/)"}}
{"actor":{"display_login":"JeffyLu"}}