###一、命令行工具###
django-admin.py是Django的一个用于管理任务的命令行工具,manage.py是对django-admin.py的简单包装,每个Django Project里面都会包含一个manage.py
语法:
1root># django-admin.py <subcommand> [options] 2 3root># manage.py <subcommand> [options] 4 5subcommand是字命令; options是可选的
###二、常用子命令###
11、新建一个django项目 2django-admin.py startproject project-name 3 4 52、新建一个app 6python manage.py startapp app-name 7 8 93、运行开发服务器 10python manage.py runserver 0.0.0.0:8000 11或 12python manage.py runserver 0:8000 13 14 154、同步数据库 16python manage.py makemigrations --生成数据库同步脚本 17python manage.py migrate --同步数据库 18 19注意:Django 老版本可以使用用syncdb 20python manage.py syncdb 21 22 235、清空数据库 24python manage.py flush 25 26此命令会询问是 yes 还是 no, 选择 yes 会把数据全部清空掉,只留下空表。 27 28 296、创建超级管理员 30python manage.py createsuperuser 31 32 337、导出数据、导入数据 34python manage.py dumpdata appname > appname.json 35python manage.py loaddata appname.json 36 37 388、django项目环境终端 39python manage.py shell 40 41如果你安装了bpython或者ipython,会自动调用他们的界面 42 43 449、数据库执行命令 45python manage.py dbshell 46 47django会进行到settings中设置的数据库,如果是mysql或者postgresql,会要求输入用户名和密码 48在这个终端可以输入sql语句 49 5010、编译语言文件 51python manage.py compilemessages 52 5311、创建语言文件 54python manage.py makemessages 55 56
###三、manage.py特有的一些子命令
11、创建超级管理员 2python manage.py createsuperuser 3 4 52、修改密码 6python manage.py changepassword 7 83、清除sessions 9python manage.py clearsessions 10
四、查看帮助
11、查看django-admin.py帮助文档 2root># django-admin.py help startproject 3 4usage: django-admin.py startproject [-h] [--version] [-v {0,1,2,3}] 5 [--settings SETTINGS] 6 [--pythonpath PYTHONPATH] [--traceback] 7 [--no-color] [--template TEMPLATE] 8 [--extension EXTENSIONS] [--name FILES] 9 name [directory] 10 11Creates a Django project directory structure for the given project name in the 12current directory or optionally in the given directory. 13 14positional arguments: 15 name Name of the application or project. 16 directory Optional destination directory 17 18optional arguments: 19 -h, --help show this help message and exit 20 --version show program's version number and exit 21 -v {0,1,2,3}, --verbosity {0,1,2,3} 22 Verbosity level; 0=minimal output, 1=normal output, 23 2=verbose output, 3=very verbose output 24 --settings SETTINGS The Python path to a settings module, e.g. 25 "myproject.settings.main". If this isn't provided, the 26 DJANGO_SETTINGS_MODULE environment variable will be 27 used. 28 --pythonpath PYTHONPATH 29 A directory to add to the Python path, e.g. 30 "/home/djangoprojects/myproject". 31 --traceback Raise on CommandError exceptions 32 --no-color Don't colorize the command output. 33 --template TEMPLATE The path or URL to load the template from. 34 --extension EXTENSIONS, -e EXTENSIONS 35 The file extension(s) to render (default: "py"). 36 Separate multiple extensions with commas, or use -e 37 multiple times. 38 --name FILES, -n FILES 39 The file name(s) to render. Separate multiple 40 extensions with commas, or use -n multiple times. 41 422、查看manage.py帮助 43 44root># python manage.py help 45 46Type 'manage.py help <subcommand>' for help on a specific subcommand. 47 48Available subcommands: 49 50[auth] 51 changepassword 52 createsuperuser 53 54[contenttypes] 55 remove_stale_contenttypes 56 57[django] 58 check 59 compilemessages 60 createcachetable 61 dbshell 62 diffsettings 63 dumpdata 64 flush 65 inspectdb 66 loaddata 67 makemessages 68 makemigrations 69 migrate 70 sendtestemail 71 shell 72 showmigrations 73 sqlflush 74 sqlmigrate 75 sqlsequencereset 76 squashmigrations 77 startapp 78 startproject 79 test 80 testserver 81 82[sessions] 83 clearsessions 84 85[staticfiles] 86 collectstatic 87 findstatic 88 runserver