Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Django 常用命令 #154

@qingquan-li

Description

@qingquan-li

一、创建Django项目

https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial01/#creating-a-project

$ django-admin startproject project_name

# 目录结构:
project_name
├── manage.py
└── project_name
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py
# 相比以上新建项目的方式,更推荐使用以下方式新建项目:
$ mkdir project_folder_name
# 符号"."将告诉脚本程序自动安装 Django 到你当前选择的目录中
project_folder_name $ django-admin startproject project_name .

# 目录结构:
project_folder_name 
├── manage.py
└── project_name
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

注意事项:

  • 需要先安装好 Django ,例如安装 Django 2.2 版本:$ pip install django=2.2

  • 如果使用 Git 来管理项目,请先在项目的根目录下新建 .gitignore 文件,再使用 Git 提交代码。

  • Git 提交代码之前,先安装 python-dotenv ,将 setting.py 中的敏感信息(例如密码),存放到在 .env 配置文件中(不进行 Git 提交)。类似于将敏感信息存放到环境变量配置中。


二、启动用于开发的简易服务器

https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial01/#the-development-server

$ cd project_name  # 进入 Django 项目路径(manage.py 文件所在的目录)
# 使用默认8000端口,等价于执行 $ python manage.py runserver 127.0.0.1:8000
$ python manage.py runserver
# 使用其他域名和端口,启动服务器:
$ python manage.py runserver 0.0.0.0:<your port>

三、创建app应用

https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial01/#creating-the-polls-app

$ python manage.py startapp app_name

#目录结构:
project_folder_name 
├── manage.py
└── project_name
    ├── __init__.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py
    app_name
    ├── __init__.py
    ├── admin.py
    ├── apps.py
    ├── models.py
    ├── tests.py
    ├── views.py
    └── migrations
        └── __init__.py


If you’re starting a new project, it’s highly recommended to set up a custom user model:
https://docs.djangoproject.com/en/dev/topics/auth/customizing/#substituting-a-custom-user-model

$ python manage.py startapp accounts
# Creating the custom User model and point AUTH_USER_MODEL to it in `settings` file
# before creating any migrations or running manage.py migrate for the first time.

# If you have executed the first `$ python manage.py migrate` command
# after initializing the Django project and before creating a custom user model:
# reference:https://stackoverflow.com/questions/44651760/
# 1. Delete the _pycache_ and the 0001_initial files.
# 2. Delete the db.sqlite3 from the root directory.
# 3. Rerun: $ python manage.py makemigrations and $ python manage.py migrate

class CustomUser(AbstractUser):
    pass

四、数据库迁移

https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial02/

$ python manage.py makemigrations [app_name]
$ python manage.py migrate [app_name]

迁移是非常强大的功能,它能让你在开发过程中持续的改变数据库结构而不需要重新删除和创建表 - 它专注于使数据库平滑升级而不会丢失数据。改变模型需要这三步:

  1. 编辑 models.py 文件,改变模型;
  2. 运行 $ python manage.py makemigrations 为模型的改变生成迁移文件;
  3. 运行 $ python manage.py migrate 来应用数据库迁移。

数据库迁移被分解成生成和应用两个命令是为了让你能够在代码控制系统上提交迁移数据并使其能在多个应用里使用;这不仅仅会让开发更加简单,也给别的开发者和生产环境中的使用带来方便。


Note:
Don't recommend running $ python manage.py migrate on new projects until after a custom user model has been configured.
Otherwise Django will bind the database to the built-in User model which is difficult to modify later on in the project.
References:https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#substituting-a-custom-user-model


1. 开发迭代部署过程中的数据库迁移

如果项目开发迭代过程中,编辑了 models.py 文件,改变了模型;并且执行了 $ python manage.py makemigrations 为模型的改变生成迁移文件,迁移文件自动生成在 app_name/migrations 目录下。

那么,把代码部署到服务器后需要执行 $ python manage.py migrate 命令,来应用迁移文件,从而在 django_migrations 数据库表中生成对应的数据库迁移记录(假设开发时连接的是本地开发环境的数据库)。

注意:该命令不一定要在服务器中执行,可以在“把代码部署到服务器”之前,在本地开发环境连接服务器生产环境的数据库,执行该命令,然后就会在服务器生产环境的数据库表 django_migrations 中生产对应的记录。


2. 重置Django migration

  1. 删除 app_name/migrations 下的 000x_auto_yyyyMMdd_hhmm.py
  2. 删除数据库表 django_migrations 中对应的记录

注意:如果需要使用 MySQL 替代默认的 SQLite 数据库,需要先为 Django 项目创建 MySQL 数据库,参考:Django 项目配置 MySQL

:伪造数据库迁移参考:#143


五、交互式 Python 命令行

https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial02/#playing-with-the-api

$ python manage.py shell

我们使用这个命令而不是简单的使用 "Python" ,是因为 manage.py 会设置 DJANGO_SETTINGS_MODULE 环境变量,这个变量会让 Django 根据 app_name/settings.py 文件来设置 Python 包的导入路径。


附:执行 Django 项目中的指定文件

$ python manage.py shell
>>> import app_name.module_name

六、创建一个管理员账号

https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial02/#creating-an-admin-user

$ python manage.py createsuperuser
Username: admin
Email address: [email protected]
Password: **********
Password (again): *********
Superuser created successfully.

七、收集静态文件

https://docs.djangoproject.com/zh-hans/2.2/howto/static-files/

$ python manage.py collectstatic

把静态目录下的所有文件拷贝至 settings.py 中 STATIC_ROOT 设置的目录。
一般在部署项目到服务器后,更改了静态文件后使用。


八、运行测试

https://docs.djangoproject.com/zh-hans/2.2/intro/tutorial05/#running-tests
https://docs.djangoproject.com/en/3.2/topics/testing/overview/#running-tests

$ python manage.py test app_name

按照惯例,Django 应用的测试应该写在应用的 tests.py 文件里。测试系统会自动的在所有以 tests 开头的文件里寻找并执行测试代码。

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions