8个常用Django命令

8个常用的Django命令

8个常用的Django命令

8个常用的Django命令

  在本Python教程中,我们将讨论初级Django开发人员应该知道8个常用的Django命令。事不宜迟,让我们开始使用不同的 Django 命令,初学者必须了解这些命令才能轻松完成工作,虽然您总是可以走传统路线,使用鼠标和键盘,并使用 GUI 工作。

1、创建一个Django项目

  如果您熟悉 Django,您就会知道 Django 提供了初始文件、文件夹和设置的集合来启动我们的项目。要创建初始 Django 项目结构,请打开您的操作系统终端并打开您cd要保存 Django 项目代码的目录或文件夹。然后在所选终端上运行以下命令,这将在当前工作目录中使用命令中提供的 <project_name> 创建一个目录/文件夹。

django-admin startproject myproj

输出:

创建一个Django项目
8个常用的Django命令

2、生成迁移命令

  将为模型类(进一步表示数据库中的表)编写的 Python 代码转换为数据库查询。每当我们对数据库类模型进行任何类型的更改时,都必须运行此命令。要运行以下命令,请在项目文件夹中移动,该文件夹包含manage.py将在主项目目录中创建必要数据库文件的文件。

python manage.py makemigrations

输出:

生成迁移命令
8个常用的Django命令

3、迁移命令

  我们需要运行这个命令,根据定义的 Python 类模型在指定的数据库中创建表。此命令负责应用或取消应用迁移。当我们第一次运行这个命令时,所有与默认应用程序(由 Django 框架提供)相关的迁移都会被应用。

python 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 auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying sessions.0001_initial... OK

4、收集静态文件

  在 Django 中,我们以不同的方式处理静态文件。建议创建一个单独的文件夹并将所有静态文件保存在那里。我们需要这些 Django 命令来让它知道主项目目录中存在的静态文件。

python manage.py collectstatic

输出:

You have requested to collect static files at the destination
location as specified in your settings.
 
This will overwrite existing files!
Are you sure you want to do this?
 
Type 'yes' to continue, or 'no' to cancel:

5、创建一个 Django 应用

  Django 项目是网站的应用程序和配置的集合。一个项目中可以有多个应用程序,一个应用程序可以包含在多个 Django 项目中。在 Django 项目中创建 Django 应用程序需要此命令,该应用程序将生成 Django 应用程序的基本目录结构。

python manage.py startapp myapp

输出:

创建一个Django应用
8个常用的Django命令

6、创建超级用户

  登录Django框架提供的默认管理界面面板是必不可少的命令。需要此命令为 Admin 界面创建一个超级用户,该超级用户拥有用户名、密码和所有其他访问和管理 Django 网站所需的权限。

python manage.py createsuperuser

输出:

Username (leave blank to use 'inspiron'): Username
Email address: example@mail.com
Password: 
Password (again): 
This password is too short. It must contain at least 8 characters.
This password is too common.
This password is entirely numeric.
Bypass password validation and create user anyway? [y/N]: y
Superuser created successfully.

7、更改密码

  有可能,我们忘记了默认管理界面面板的密码。然后非常有必要重置它,因为没有密码我们将无法访问默认的管理界面面板。我们必须提供适当的 <username>,在运行此命令时必须重置其密码。

python manage.py changepassword <username>

输出:

Changing password for user 'Username'
Password: 
Password (again): 
Password changed successfully for user 'Username'

8、运行服务器

  它再次成为非常重要和最常用的 Django 命令之一。我们需要这个命令来验证和测试我们的 Django 应用程序和网站,方法是在本地服务器上运行它们。默认情况下,此命令在端口号 8000 的内部 IP 上运行 Django 开发服务器。如果需要,我们还可以通过将它们作为命令行参数发送来更改开发服务器的 IP 和端口号。

python manage.py runserver

输出:

Watching for file changes with StatReloader
Performing system checks...
 
System check identified no issues (0 silenced).
August 30, 2021 - 15:16:23
Django version 3.2.5, using settings 'myproj.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

结论

  以上是晓得博客为你介绍的 8个常用的Django命令的全部内容,在本Python教程中,我们学习了初级Django开发人员应该知道的8个常用的Django命令。希望你已经理解了上面讨论的概念,并准备好构建你自己的 Django 网站或应用程序。

  推荐:Pycharm Django

给文章评分

晓得博客,版权所有丨如未注明,均为原创
晓得博客 » 8个常用的Django命令

转载请保留链接:https://www.pythonthree.com/8-django-commands/

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部