创建你的第一个项目

本人属于野路子出身,IT方面全靠自学,技术一般,但心里却一直有一个开源梦,想自己独立开发一个项目然后放到github上。想了很久最终决定做一个运维平台,因为现在工作方向还是偏运维,对这块会熟悉一点。对于运维平台的话,总体的需求一般就是以下个方面监控告警、资产管理、自动化部署上线、配置管理等。如果想做的非常棒,以上每个需求都可以单独拿出来做一个项目的。

创建第一个项目

在命令行中,cd到你想保存项目的目录,然后执行以下命令:

E:\opgit>C:\Python35\Scripts\django-admin.exe startproject mixcloud

E:\opgit>dir E:\opgit\mixcloud

2017/03/05  12:41              .
2017/03/05  12:41              ..
2017/03/05  12:41               806 manage.py
2017/03/05  12:41              mixcloud

E:\opgit>dir E:\opgit\mixcloud\mixcloud
2017/03/05  12:41              .
2017/03/05  12:41              ..
2017/03/05  12:41             3,102 settings.py
2017/03/05  12:41               765 urls.py
2017/03/05  12:41               394 wsgi.py
2017/03/05  12:41                 0 __init__.py
               4 个文件          4,261 字节
               2 个目录 100,502,478,848 可用字节

windows的dos命令行,没有linux命令行看的舒服。但是为了能用pycharm开发,只能将就一下。
执行完后,将会在当前目录生成一个mixcloud目录。目录结构如下:

mixcloud/
    manage.py
    mixcloud/
        __init__.py
        settings.py
        urls.py
        wsgi.py

简单描述这些目录和文件:

  • 外层的mixcloud/根目录仅仅是项目的一个容器。它的命名对Django无关紧要;你可以把它重新命名为任何你喜欢的名字。
  • manage.py:一个命令行工具,可以使你用多种方式对Django项目进行交互。输入python manage.py help,可查看帮助,除了常规的查看版本外,还支持特别多的子命令,每个子命令又可通过python manage.py help ,子命令里用的比较多的一般是shell、runserver、sqlall、syncdb、createsuperuser、collectstatic,其他的用的不多。
E:\opgit\mixcloud>python manage.py help
Usage: manage.py subcommand [options] [args]

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=verbose output, 3=very verbose output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Print traceback on exception
  --version             show program's version number and exit
  -h, --help            show this help message and exit

Type 'manage.py help ' for help on a specific subcommand.

Available subcommands:

[auth]
    changepassword
    createsuperuser

[django]
    check
    dbshell
    shell
    sql
    sqlall
    startapp
    startproject
    syncdb
    ...
[sessions]
    clearsessions

[staticfiles]
    collectstatic
    findstatic
    runserver
  • 内层的mysite/目录,是你的项目的真正的Python包。
    它是你导入任何东西时将需要使用的Python包的名字(例如mysite.urls
    )。

  • mysite/init.py:一个空文件,它告诉Python这个目录应该被看做一个Python包。

  • mysite/settings.py:该Django 项目的设置/配置文件。Django 设置 将告诉你这些设置如何工作。

  • mysite/urls.py:该Django项目的URL声明;可看作是Django站点的“目录”。

  • mysite/wsgi.py:用于你的项目的与WSGI兼容的Web服务器入口。

初始化配置

项目创建好,还需要对项目进行一些初始化的配置。比如数据库、时区等
设置数据库

数据库配置放在mixcloud/settings.py,它是一个用模块级别变量表示 Django 配置的普通 Python 模块。
默认情况下,数据库配置使用的是SQLite,一个轻便的文件类型数据库,如果你只是简单学习,想快速将django项目跑起来,SQLite是最好的选择。默认配置如下

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

这里我用mysql作为该项目的数据库。编辑mixcloud/settings.py文件,修改如下DATABASES配置

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql', 
            'HOST': 'localhost',  # 数据库所在服务器
            'NAME': 'mixcloud',  # 数据库名
            'USER': 'root',   # 连接数据库的用户
            'PASSWORD': '',   # 密码
        },
    }

配置好后,就可以创建数据库了。

E:\opgit\mixcloud>mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 426
Server version: 5.6.34 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database mixcloud default character set utf8;
Query OK, 1 row affected (0.08 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| cmdb               |
| mixcloud           |
| mysql              |
| ossdb              |
| performance_schema |
| test               |
+--------------------+
7 rows in set (0.05 sec)

mysql>

设置时区

django默认的时区是UTC,如果不设置,显示的时间会与中国实际的时间相差8小时。编辑mixcloud/settings.py
时,为中国的时区TIME_ZONE = 'Asia/Shanghai'

配置APP

默认情况下,INSTALLED_APPS
会包含以下应用,这些应用是django自带的,开发人员可以直接使用,也可以将其打包分发到其他项目中使用。

  • django.contrib.admin: 管理站点。一个功能丰富的后台管理系统

  • django.contrib.auth:认证系统。方便的用户管理和权限管理

  • django.contrib.contenttypes: 用于内容类型的框架。

  • django.contrib.sessions:会话框架。用于session管理

  • django.contrib.messages:消息框架。用于用户进行操作页面后的提示信息

  • django.contrib.staticfiles:管理静态文件的框架。

对于崇尚大道至简的开发者,如果不需要其中任何一个或所有应用,可以在syncdb之前,将对应的app注释即可。

生成apps所需要的表

syncdb命令将查看INSTALLED_APPS设置并根据settings.py文件的数据库设置,创建任何必要的数据库表。运行以下命令:

python manage.py syncdb

E:\opgit\mixcloud>python manage.py syncdb
Creating tables ...
Creating table django_admin_log
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table django_content_type
Creating table django_session

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (leave blank to use 'max352'): Michaelzeng
E-mail address: [email protected]
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)

第一次创建表使用的是syncdb命令,以后如果表结构有变或新增app,则是用migrage命令操作。
从输出可以看到创建了很多表,以及创建了一个superuser。我们可登录数据库后台,查看新建的表。

运行开发服务器

django开发服务器是可用在开发期间的,一个内建的,轻量的web服务。这样我们就可以在开发过程中快速的启动服务,并进行调试。

进入外层mixcloud目录,运行命令

E:\opgit\mixcloud>python35 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 13 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 05, 2017 - 19:41:49
Django version 1.10.2, using settings 'mixcloud.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

我们访问http://127.0.0.1:8000/ ,如果可以看到django自带的欢迎界面,表示已经可以正常工作了

创建你的第一个项目_第1张图片
Paste_Image.png

虽然django自带的web服务,非常方便,但是在生产环境中,不建议使用,在同一时间,该服务器只能可靠地处理一次单个请求,并且没有进行任何类型的安全审计。默认情况下,runserver 命令在 8000 端口启动开发服务器,且仅监听本地连接。 要想要更改服务器端口的话,可将端口作为命令行参数传入:

python35 manage.py runserver 8080

但是以上方式启动的web服务,仅能本地访问。如果你想和其他开发人员共享同一开发站点的话,该功能特别有用。 0.0.0.0 这个 IP 地址,告诉服务器去侦听任意的网络接口。

python35 manage.py runserver 0.0.0.0:8080

这样,你本地网络中的其它计算机就可以在浏览器中通过 IP 地址:端口来访问web服务了。

你可能感兴趣的:(创建你的第一个项目)