Django 学习日记 - django-admin.py&manage.py - step1

1 知识点

  1. django-admin.py是Django的一个用于管理任务的命令行工具,
  2. manage.py是对django-admin.py的简单包装,每个Django Project里面都会包含一个manage.py,要创建项目才会有

2 语法

django-admin.py  [options]
manage.py   [options]

subcommand是子命令;options是可选的

常用子命令:
startproject: 创建一个项目 (*)
startapp:创建一个app (*)
runserver:运行开发服务器 (*)
shell:进入django shell (*)
dbshell:进入django dbshell
check:检查django项目完整性
flush:清空数据库
compilemessages:编译语言文件(*)
makemessages:创建语言文件(*)
makemigrations:生成数据库同步脚本(第一次使用) (*)
migrate:同步数据库 (*)
showmigrations:查看生成的数据库同步脚本 (*)
sqlflush:查看生成清空数据库的脚本 (*)
sqlmigrate:查看数据库同步的sql语句 (*)
dumpdata:导出数据
loaddata:导入数据
diffsettings: 查看你的配置和django默认配置的不同之处

3 获取帮助文档

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\class04\hello_django>django-admin.py help startproject
usage: django-admin.py startproject [-h] [--version] [-v {0,1,2,3}]
                                    [--settings SETTINGS]
                                    [--pythonpath PYTHONPATH] [--traceback]
                                    [--no-color] [--template TEMPLATE]
                                    [--extension EXTENSIONS] [--name FILES]
                                    name [directory]

Creates a Django project directory structure for the given project name in the
current directory or optionally in the given directory.

positional arguments:
  name                  Name of the application or project.
  directory             Optional destination directory

optional arguments:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -v {0,1,2,3}, --verbosity {0,1,2,3}
                        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           Raise on CommandError exceptions
  --no-color            Don't colorize the command output.
  --template TEMPLATE   The path or URL to load the template from.
  --extension EXTENSIONS, -e EXTENSIONS
                        The file extension(s) to render (default: "py").
                        Separate multiple extensions with commas, or use -e
                        multiple times.
  --name FILES, -n FILES
                        The file name(s) to render. Separate multiple
                        extensions with commas, or use -n multiple times.

4 manage.py特有的一些子命令

createsuperuser: 创建超级管理员 (*)
changepassword: 修改密码 (*)
clearsessions: 清除session

5 实验步骤

5.1 创建project和创建app

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04>django-admin.py startproject hello_django
(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04>cd hello_django
(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04\hello_django>django-admin.py startapp hello

5.2 启动项目并测试

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04\hello_django>manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
January 09, 2017 - 22:21:15
Django version 1.9.7, using settings 'hello_django.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Django 学习日记 - django-admin.py&manage.py - step1_第1张图片
访问测试

5.2 同步数据库

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04\hello_django>manage.py makemigrations
No changes detected

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\learn04\hello_django>manage.py migrate
Operations to perform:
  Apply all migrations: admin, contenttypes, auth, sessions
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying sessions.0001_initial... OK

5.3 创建管理员账户登录管理后台

(env_py35_django) D:\MaiZi_Edu\Dropbox\Maizi\Django\class04\hello_django>manage.py createsuperuser
Username (leave blank to use 'spareribs'): admin
Email address: [email protected]
Password:
Password (again):
Superuser created successfully.
Django 学习日记 - django-admin.py&manage.py - step1_第2张图片
登陆admin管理后台

你可能感兴趣的:(Django 学习日记 - django-admin.py&manage.py - step1)