部署(Django )

   

    Git 是一个被大量程序员使用的"版本控制系统"。 此软件可以跟踪任何时间文件的改变,这样你以后可以随时召回某个特定版本。

    把我们的网站放到一个服务器PythonAnywhere上;使用到的外部服务器是GitHub,它是一个代码托管服务。现在要使用 GitHub 作为基石,和 PythonAnywhere 互相传输我们的代码。

1.安装Git

    从 git-scm.com 下载Git,安装时一直选择 “NEXT” ,除了在:"Adjusting your PATH environment",需要选择"Run Git and associated Unix tools from the Windows command-line"(底部的选项)。

2.开始自己的Git版本库

    从已安装的Git文件夹中打开git-cmd.exe,进入djangogirls文件夹,输入如下命令:

$ git init   #初始化仓库
Initialized empty Git repository in ~/djangogirls/.git/
$ git config --global user.name "Your Name"
$ git config --global user.email you@example.com

    每个项目我们只需要初始化一次Git仓库(而且你从此不需要重新输入用户名和邮箱),Git会追踪这个目录下所有文件和文件夹的更改,但是有一些文件我们希望Git忽略它:在djangogirls项目根目录下创建一个命名为 .gitignore 的文件。

    打开编辑器,创建新文件并写入以下内容:

*.pyc
__pycache__
myvenv   #记得改为你的虚拟环境的名字
db.sqlite3
.DS_Store

    在执行git操作之前,最好使用 git status 命令查看一下当前的状态,尤其是在执行 git add或者在你不确定哪些文件被改动的情况下。 这有助于阻止各种意外发生,例如错误的文件被添加或提交。 git status 命令会返回所有未追踪/修改/暂存的文件,还有分支状态等信息。 输出会是这样:

$ git status
On branch master

Initial commit

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        .gitignore
        blog/
        manage.py
        mysite/

nothing added to commit but untracked files present (use "git add" to track)

    保存更改,转到git-cmd.exe控制台并运行这些命令:

$ git add --all .  #将文件添加到仓库
$ git commit -m "My Django Girls app, first commit"  #将文件提交到仓库
[...]
13 files changed, 200 insertions(+)
create mode 100644 .gitignore
[...]
create mode 100644 mysite/wsgi.py

3.推送我们的代码到Github上

    创建一个新的仓库,命名为“my-first-blog”。 保持 "initialise with a README" 复选框未选中状态,.gitignore 选项为无 (我们已经手动创建了) ,让License设置为无。

    在下一屏中,你将看到你的仓库克隆 URL。选择“HTTPS”版本,拷贝地址,把它粘贴到git-cmd.exe终端:

$ git remote add origin https://github.com/<your-github-username>/my-first-blog.git      #替换' <your-github-username> '为你的 github 用户名 
$ git push -u origin master   #第一次推送master分支的所有内容

    你会看到:

                   

    你的代码已经在Github上了:

4.在 PythonAnywhere 设置博客

    在PythonAnywhere注册一个“Beginner”账户:www.pythonanywhere.com

5.在 PythonAnywhere 上拉取我们的代码

    选择启动“Bash”控制台这一选项 — 这是 PythonAnywhere 版的控制台,就像你本地电脑上的一样。

    通过创建一个我们仓库的 “Clone” 以便从 Github 拉取代码到 PythonAnywhere。 在 PythonAnywhere 控制台输入:

$ git clone https://github.com/<your-github-username>/my-first-blog.git  #将"<your-github-username>"换为自己GitHub的用户名

    这将会拉取一份你的代码副本到 PythonAnywhere 上

6.在 PythonAnywhere 上创建 virtualenv

   在 Bash 控制台下,键入:

$ cd my-first-blog

$ virtualenv --python=python3.4 myvenv
Running virtualenv with interpreter /usr/bin/python3.4
[...]
Installing setuptools, pip...done.

$ source myvenv/bin/activate

(mvenv) $  pip install django whitenoise
Collecting django
[...]
Successfully installed django-1.8.2 whitenoise-2.0

7.收集静态文件

    什么是"whitenoise"白噪音? 它是用来服务所谓的“static files”静态文件的工具,静态文件是很少改动或者并非可运行的程序代码的那些文件,比如 HTML 或 CSS 文件。

    暂且我们只需要在服务器上运行一个额外的命令,就是collectstatic, 它告诉 Django 去收集服务器上所有需要的静态文件, 眼下来说主要是使admin管理界面看起来更漂亮的文件:

(mvenv) $ python manage.py collectstatic

8.在 PythonAnywhere 上创建数据库

    可以像在自己的计算机上一样在服务器上初始化数据库,使用migrate 和 createsuperuser:

(mvenv) $ python manage.py migrate
Operations to perform:
[...]
  Applying sessions.0001_initial... OK


(mvenv) $ python manage.py createsuperuser

Context | Request Context

9.将博客发布为一个网络应用程序

① 设置 virtualenv

    在 “Virtualenv” 一节,点击红色文字 “Enter the path to a virtualenv",然后键入:/home/<your-username>/my-first-blog/myvenv/,点击保存。

② 配置 WSGI 文件

     Django 使用 “WSGI 协议”,它是用来服务 Python 网站的一个标准。 PythonAnywhere 通过配置 WSGI 配置文件来识别Django 博客。

    点击 “WSGI configuration file” 链接(在 "Code" 一节,接近页面上方),然后跳转到一个编辑器,删除所有的内容并用以下内容替换:

import os
import sys

path = '/home/<your-username>/my-first-blog'  # 用自己的用户名替代<your-username>
if path not in sys.path:
    sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings' #用自己文件夹的名字代替mysite

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(get_wsgi_application())

    这个文件的作用是:告诉 PythonAnywhere 我们的Web应用程序在什么位置,Django 设置文件的名字是什么,也设置 "whitenoise" 静态文件工具。

    点击 Save 然后返回到 Web 选项卡。

③ 上线了

    点击绿色 Reload 按钮然后你将会看到你的应用程序。页面的顶部可以看到它的链接。

原文地址:https://www.cnblogs.com/llw1121/p/6957039.html