Use of shell commands in Django

Time:2021-7-30

Shell tools

Django’s manage tool provides shell commands to help us configure the running environment of the current project (such as connecting to the database), so that we can directly execute test Python statements in the terminal.

Enter the shell with the following command


python manage.py shell

在这里插入图片描述

Import two model classes for later use


from book.models import BookInfo,PeopleInfo

The shell in this article is a command of the manage tool

Remember that the administrator created earlier also used the manage command. Let’s talk about the manage command

Django’s command line tools

Django-admin.py is a command-line tool of Django for managing tasks. Manage.py is a simple package of django-admin.py. Each Django project will contain a manage.py


django-admin.py <subcommand> [options]
manage.py <subcommand> [options]

Subcommand is a subcommand; Options is optional

#Common subcommands:

Startproject: create a project (*)
Startapp: create an app (*)
Runserver: run the development server (*)
Shell: enter Django shell (*)
Dbshell: enter the Django dbshell
Check: check the integrity of Django project
Flush: empty database
Compile messages: compile language files
Make messages: create language files
Make migrations: generate database synchronization scripts (*)
Migrate: synchronize database (*)
Show migrations: view the generated database synchronization script (*)
Sqlflush: view the script generated to empty the database (*)
Sqlmigrate: View SQL statements for database synchronization (*)
Dumpdata: export data
Loaddata: import data
Diffsettings: see the differences between your configuration and Django's default configuration

Some subcommands unique to manage.py:

Createsuperuser: create super administrator (*)
ChangePassword: Change Password (*)
Clearsessions: clear sessions

Change the port of the development server:


python manage.py runserver 8080

To view help documentation:


python manage.py help

D:\day70class>python manage.py help

Type '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

This is the end of this article on the use of shell commands in Django. For more information about Django shell commands, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!