First, the new project
$django-admin.py startproject project_name # 特别是在 windows 上,如果报错,尝试用 django-admin 代替 django-admin.py 试试
Note that Project_Name is the name of its own project and needs to be a valid Python package name, such as cannot be 1a or a-B.
Second, the new app
To enter the project directory first, CD Project_Name then execute the following command;
$ python manage.py startapp app_name或 django-admin.py startapp app_name
Typically, a project has multiple apps, and of course a common app can be used in multiple projects.
Similar to the project name App name also needs to be a legitimate Python package name, such as blog,news,aboutus, etc. are legitimate app names;
Iii. Creating a database table or changing a datasheet or field
Django 1.7.1及以上 用以下命令# 1. 创建更改的文件$python manage.py makemigrations# 2. 将生成的py文件应用到数据库$python manage.py migrate 旧版本的Django 1.6及以下用$python manage.py syncdb
This method can create tables in databases such as SQL that correspond to the models.py code, without having to manually execute the SQL
Iv. use of the development server
Development server, that is, the development of the use of general code changes will be automatically restarted, convenient debugging and development, but due to performance issues, it is recommended only for testing, do not use in the production environment.
$python manage.py runserver # 当提示端口被占用的时候,可以用其它端口:$python manage.py runserver 8001$python manage.py runserver 9999(当然也可以kill掉占用端口的进程,具体后面有讲,此处想知道的同学可查下 lsof 命令用法) # 监听机器所有可用 ip (电脑可能有多个内网ip或多个外网ip)$python manage.py runserver 0.0.0.0:8000# 如果是外网或者局域网电脑上可以用其它电脑查看开发服务器# 访问对应的 ip加端口,比如 http://172.16.20.2:8000
Five, clear the database
$python manage.py flush
This command asks yes or no, and selecting Yes will erase the data, leaving only the empty table
VI. Create Super Admin
$python manage.py createsuperuser# 按照提示输入用户名和对应的密码就好了邮箱可以留空,用户名密码必填# 修改 用户密码可以用:$python manage.py changepassword username
VII. Export data, import data
$python manage.py dumpdata appname > appname.json$python manage.py loaddata appname.json
VIII. Django Project Environment terminal
$python manage.py shell
If you installed Bpython or Ipython will automatically use their interface, it is recommended to install Bpython.
The difference between this command and the direct run of Python or bpython into the shell is that you can invoke the API from the current project's models.py in this shell, and there are some small tests handy for manipulating the data.
Ix. Database command line
$python manage.py dbshell
Django automatically enters the database set up in settings.py and, if it is MySQL or PostgreSQL, requires a database user password.
The SQL statement of the database can be executed at this terminal. If you're familiar with SQL, you might like this way.
Ten, more orders
? mysite python3.6 manage.pyType 'manage.py help <subcommand>' for help on a specific subcommand.Available subcommands:[auth] changepassword createsuperuser[contenttypes] remove_stale_contenttypes[django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test testserver[sessions] clearsessions[staticfiles] collectstatic findstatic runserver
Python's basic Django command