辉_少

Django基本命令

Django基本命令:

1、创建Django项目:

 root@zh-PC:~# django-admin startproject django01 

注意: django01 为创建的项目名称,一定要符合Django的命名规范

2、创建django APP:

 root@zh-PC:~# django-admin startapp study 

注意: study为创建的APP名称,一般一个项目有多个app, 当然通用的app也可以在多个项目中使用

3、创建数据库表 或 更改数据库表或字段

  注意:Django 1.7.1及以上版本使用一下命令

 3.1 创建更改的文件

root@zh-PC:~# ls
django01  study
root@zh-PC:~# cd django01/
root@zh-PC:~/django01# python3 manage.py makemigrations
No changes detected
root@zh-PC:~/django01# ls
db.sqlite3  django01  manage.py

 

 3.2 将生成的py文件应用到数据库

root@zh-PC:~/django01# ls
db.sqlite3  django01  manage.py
root@zh-PC:~/django01# python3 manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... 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 auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying sessions.0001_initial... OK

这种方法可以在SQL等数据库中创建与models.py代码对应的表,不需要自己手动执行SQL。

备注:对已有的 models 进行修改,Django 1.7之前的版本的Django都是无法自动更改表结构的,不过有第三方工具 south,详见 Django 数据库迁移 一节

 

4、使用开发服务器

开发服务器,即开发时使用,一般修改代码后会自动重启,方便调试和开发,但是由于性能问题,建议只用来测试,不要用在生产环境

root@zh-PC:~/django01# python3 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2019 - 14:06:01
Django version 2.1.7, using settings 'django01.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

 

当提示端口被占用的时候,可以用其它端口::

root@zh-PC:~/django01# python3 manage.py runserver 8001
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2019 - 14:07:21
Django version 2.1.7, using settings 'django01.settings'
Starting development server at http://127.0.0.1:8001/
Quit the server with CONTROL-C.

 

 监听机器所有可用 ip (电脑可能有多个内网ip或多个外网ip

root@zh-PC:~/django01# python3 manage.py runserver 0.0.0.0:8000
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2019 - 14:09:13
Django version 2.1.7, using settings 'django01.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.
Error: That port is already in use.
root@zh-PC:~/django01# python3 manage.py runserver 0.0.0.0:8001
Performing system checks...

System check identified no issues (0 silenced).
March 16, 2019 - 14:09:41
Django version 2.1.7, using settings 'django01.settings'
Starting development server at http://0.0.0.0:8001/
Quit the server with CONTROL-C.

 

 # 如果是外网或者局域网电脑上可以用其它电脑查看开发服务器

# 访问对应的 ip加端口,比如 http://172.16.20.2:8000
 
5、清空数据库:
root@zh-PC:~/django01# python3 manage.py flush
You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the '/root/django01/db.sqlite3' database,
and return each table to an empty state.
Are you sure you want to do this?

    Type 'yes' to continue, or 'no' to cancel: yes
root@zh-PC:~/django01# 
此命令会询问是 yes 还是 no, 选择 yes 会把数据全部清空掉,只留下空表

 6、创建超级管理员:

root@zh-PC:~/django01# python3 manage.py createsuperuser
Username (leave blank to use 'root'): zh
Email address: 2318**********@qq.com
Password: 
Password (again): 
Superuser created successfully.
# 按照提示输入用户名和对应的密码就好了邮箱可以留空,用户名和密码必填

 

 修改用户密码:

root@zh-PC:~/django01# python3 manage.py changepassword zh
Changing password for user 'zh'
Password: 
Password (again): 
Password changed successfully for user 'zh'

#注意:密码必须为8位以上才可以修改成功

 

7、导出数据 导入数据

#python3 manage.py dumpdata appname > appname.json  #导出数据

#python3 manage.py loaddata appname.json  #导入数据

 

8、Django 项目环境终端

root@zh-PC:~/django01# python3 manage.py shell
Python 3.6.5 (default, May 11 2018, 13:30:17) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 

 

如果你安装了 bpython 或 ipython 会自动用它们的界面,推荐安装 bpython。

这个命令和 直接运行 python 或 bpython 进入 shell 的区别是:你可以在这个 shell 里面调用当前项目的 models.py 中的 API,对于操作数据,还有一些小测试非常方便

 

9、数据库命令行

 #python3 manage.py dbshell 

Django 会自动进入在settings.py中设置的数据库,如果是 MySQL 或 postgreSQL,会要求输入数据库用户密码。

在这个终端可以执行数据库的SQL语句。如果您对SQL比较熟悉,可能喜欢这种方式。

10、更多命令,可以看到详细的列表,在忘记子名称的时候特别有用。

 1 root@zh-PC:~/django01# python3 manage.py 
 2 
 3 Type 'manage.py help <subcommand>' for help on a specific subcommand.
 4 
 5 Available subcommands:
 6 
 7 [auth]
 8     changepassword
 9     createsuperuser
10 
11 [contenttypes]
12     remove_stale_contenttypes
13 
14 [django]
15     check
16     compilemessages
17     createcachetable
18     dbshell
19     diffsettings
20     dumpdata
21     flush
22     inspectdb
23     loaddata
24     makemessages
25     makemigrations
26     migrate
27     sendtestemail
28     shell
29     showmigrations
30     sqlflush
31     sqlmigrate
32     sqlsequencereset
33     squashmigrations
34     startapp
35     startproject
36     test
37     testserver
38 
39 [sessions]
40     clearsessions
41 
42 [staticfiles]
43     collectstatic
44     findstatic
45     runserver

 

posted on 2019-03-16 22:30  辉_少  阅读(337)  评论(0编辑  收藏  举报