Django-admin and manage.py

Source: Internet
Author: User
Tags http redirect install django

Directory

One, Django built-in command options

    1. Check
    2. Dbshell
    3. Diffsettings
    4. Flush
    5. Makemigrations
    6. Migrate
    7. Runserver
    8. Shell
    9. Startapp
    10. Startproject
    11. Test

Ii. commands provided by the app

    1. ChangePassword
    2. Createsuperuser
    3. Clearsessions
    4. Collectstatic

Three, common parameters

    1. --pythonpath Pythonpath
    2. --settings settings
    3. --traceback
    4. --verbosity {0,1,2,3},-v {0,1,2,3}
    5. --no-color

Four, color control

V. Invoking administrative commands in code

Django-admin is a command-line toolset for managing Django, and when we successfully install Django, this command will be available in the operating system, but depending on the installation or the system environment, you may need to configure the call path. Under Linux, the command is generally located site-packages/django/bin , preferably made a link /usr/local/bin to, convenient to call. Under Windows, you can configure the system environment variables to refer to the Tutorial start section.

manage.py is a script file that is automatically generated in each Django project for managing the project and needs to be executed through the Python command.

There are three ways to perform the built-in commands that Django provides:

[Options [Options]  

The command is either Django built-in or your custom commands.

There are three ways to get help information:

django-admin help: Displays a list of usage information and commands.

django-admin help --commands: A list of all available commands.

django-admin help <command>: An introduction to the command and a list of available parameters.

django-admin version: Gets the Django version that is currently in use.

Use the --verbosity parameters django-admin to specify that the notification and debugging information be printed to the console.

One, Django built-in command options

Django provides us with a range of command options, some of which are used every day, some important, and some basic. Some of the important ones are listed below:

Call Mode:django-admin 命令选项 额外参数

1. Check

Check for common problems with the entire Django project.

By default, all apps will be selected. The specified app can be checked by providing the app's name:

Django-admin Check Auth admin MyApp

If you do not specify any of the apps, then all of the apps will be checked.

2. Dbshell

Run engine settings to specify the database engine for the command-line client, where User,password, and so on specify the connection parameters.

--database Database

Specifies the shell that opens a database. defaults to default.

3. diffsettings

Django-admin diffsettings

Shows the difference between the current settings file and the default settings for Django.

4. Flush

Django-admin Flush

Deletes all data from the database. Applied migrations are not purged. Delete only the specific data, do not delete the data table!

If you want to start and rerun all migrations from an empty database, you should delete and re-create the database and then run migrate, which will delete the original data table.

5. Makemigrations

Django-admin makemigrations [App_label [App_label ...]

Creates a new migration based on the detected model. The role of migration, more is the operation of the database, in the form of files, easy to check later, call, redo and so on. Especially with Git versioning, it doesn't know how the database changes and can only be traced and saved by the records in the migrated file.

6. Migrate

Django-admin migrate [App_label] [Migration_name]

Synchronizes the database state with the current model set and migration set. To put it bluntly, changes to the database, mainly data table design changes, in the database real execution. For example, create new, modify, delete data tables, add, modify, delete fields in a datasheet, and more.

7. Runserver

Django-admin Runserver [Addrport]

Enable the Lightweight development Web server that Django provides for us. By default, the server is running on port 8000 of IP address 127.0.0.1. If you want to customize the server port and address, you can explicitly pass an IP address and port number to it.

In Linux, if you run a script as a normal user, you may not have permission to run on a low port. The number of low-end ports (1024 or less) is reserved for super users (Root).

The Wsgi Application object used by this server is WSGI_APPLICATION set in.

Do not use this server in a production environment.

Normally, this server restarts automatically whenever the code we write changes, but it's not absolute, so in case of an accident, restart it manually every time you test.

When you start the server, the system's inspection framework checks for any visual errors in the entire project whenever your Python code changes, and if an error is detected, the error messages will be output to standard output.

Multiple servers can be started at the same time, as long as they are on different ports and executed multiple times django-admin runserver ... .

Note: The default IP is 127.0.0.1, which is not accessible by other hosts on the network, only native. To enable other computers on your network to access your development server, use your own IP address (for example, 192.168.2.1) or 0.0.0.0 or:: (Enable IPV6). You can also use a host name that contains only ASCII code.

The Django Development server, which supports multiple threads by default, can be --nothreading closed by parameters.

Here's an example of using different ports and addresses:

Port 8000 at IP address 127.0.0.1:

Django-admin Runserver

Port 8000 at IP address 1.2.3.4:

Django-admin Runserver 1.2.3.4:8000

Port 7000 at IP address 127.0.0.1:

Django-admin Runserver 7000

Port 7000 at IP address 1.2.3.4:

Django-admin Runserver 1.2.3.4:7000

Port 8000 in IPV6 Address:: 1:

Django-admin runserver-6

Port 7000 in IPV6 Address:: 1:

Django-admin runserver-6 7000

Port 7000 at IPV6 address 2001:0db8:1234:5678::9:

Django-admin Runserver [2001:0db8:1234:5678::9]:7000

Port 8000 on host IPV4 address localhost:

Django-admin Runserver localhost:8000

Port 8000 on host IPV6 address localhost:

Django-admin runserver-6 localhost:8000
8. Shell

Django-admin Shell

Launches the Python interactive interpreter with the Django environment, which is the command-line environment. The basic Python interactive interpreter is used by default. This command is very common and is an integral part of our testing and development process!

--interface {ipython,bpython,python}, -i {ipython,bpython,python}

Specifies the shell to use. By default, Django will use Ipython or Bpython. If you have two installed at the same time, specify the one you want, as follows:

Using Ipython:

Django-admin shell-i Ipython

Using Bpython:

Django-admin shell-i Bpython
9. Startapp

Django-admin startapp name [directory]

Create a new app.

By default, a series of file templates are created in this new app directory, such as models.py, views.py, admin.py, and so on.

Ten. Startproject

Django-admin startproject name [directory]

New project. By default, the new directory contains manage.py scripts and project packages (containing settings.py and other files).

One. Test

Django-admin test [Test_label [Test_label ...]

Run the test code for all installed apps.

Ii. commands provided by the app

The preceding is the command entry provided by the Django Core, and below are some of the built-in apps, such as the command items provided by Auth. They are only available when the corresponding app is enabled.

Here are some of the most important:

1. ChangePassword

Django-admin ChangePassword [< Username>]

This command is only available when the Django Authentication system (Django.contrib.auth) is installed.

This command is used to change the user's password. It prompts for a new password of two times for a given user. If the input is the same two times, it becomes the new password immediately. If you do not provide a user name, the command attempts to change the password for the user name that matches the current user.

Usage examples:

Django-admin ChangePassword Tom
2. Createsuperuser

Django-admin Creationuperuser

This command is only available when the Django Authentication system (Django.contrib.auth) is installed.

Create a Super User account (a user with all permissions). This is useful if you need to create an initial Superuser account, or if you need to generate a super user account programmatically for your site.

When run interactively, this command prompts for the password of the new super user.

When run non-interactively, the password is not set and the Superuser account will not be able to log on until the password is manually set for it.

You can use the and parameters on the command line to --username --email provide the user name and e-mail address for the new account. If none is provided, Createsuperuser prompts for input when it is run interactively.

3. Clearsessions

Django-admin clearsessions

Clears the expired session. Can be used as a cron regular job or run directly.

4. collectstatic

This command is available only if you have static files application (Django.contrib.staticfiles) installed.

For the on-line environment, when debug is set to False, the static file is centralized into one directory, providing static file support for the Web server.

This is a humble but very important command!

Three, common parameters

Each command has its own specific parameter options, but the following are the parameters that all commands can use:

1.--pythonpath Pythonpath

Adds the given file-system path to the Python module import search path (in.

If not provided, Django-admin will use the value of the PYTHONPATH environment variable.

Usage examples:

Django-admin migrate--pythonpath= '/home/djangoprojects/myproject '
2.--settings Settings

Specifies the configuration file to use. such as Mysite.settings. If not provided, Django-admin will use DJANGO_SETTINGS_MODULE the value of the environment variable.

Usage examples:

Django-admin Migrate--settings=mysite.settings
3.--traceback

When CommandError is raised, the full error stack information is displayed. By default, Django-admin displays a simple error message.

Usage examples:

Django-admin Migrate--traceback
4.--verbosity {0,1,2,3},-V

Specifies how messages are printed to the console.

    • 0 means no output.
    • 1 indicates normal output (default).
    • 2 indicates verbose output.
    • 3 indicates very verbose output.

Usage examples:

Django-admin Migrate--verbosity 2
5.--no-color

Disables the color output information. Some commands add color to the content that it outputs. For example, the error will be printed in red to the console, and the SQL statement will highlight the syntax.

Usage examples:

Django-admin Runserver--no-color
Four, color control

If your terminal supports ANSI color output, then the django-admin manage.py command will use a nice color output. If you pass the output of the command to another program, it does not use the color code.

Under Windows, the native console does not support ANSI escape sequences, so there is no color output by default. However, you can install the Ansicon third-party tool, and the Django command will detect its presence and use the color output, just like on a Unix-based platform.

The color used for syntax highlighting can be customized. Django comes with three palettes:

    • Dark for terminals that display white text on a black background. This is the default palette.
    • Light for terminals that display black text on a white background.
    • Nocolor, disables syntax highlighting.

You can specify the color palette to use by setting the DJANGO_COLORS environment variable. For example, to specify the Light palette under UNIX or os/x BASH shell, you can run the following command:

Export django_colors= "Light"

You can also customize the colors you want to use, and here are the basic rules:

Error-Primary error. Notice-a small error. Success-Success. Warning-Warning. The name of the model field in Sql_field-sql. The type of the Model field in Sql_coltype-sql. Sql_keyword-an SQL keyword. The name of the model in Sql_table-sql. HTTP_INFO-1XX HTTP Information Server response. HTTP_SUCCESS-2XX HTTP successful server response. http_not_modified-304 HTTP did not modify the server response. Http_redirect-a 3XX HTTP redirect server response other than 304. http_not_found-404 HTTP did not find the server response. Http_bad_request-4XX HTTP Error request server response except 404. HTTP_SERVER_ERROR-5XX HTTP Server error response. Migrate_heading-the title in the Migration Management command. Migrate_label-The name of the migration.

You can assign a specific foreground and background color from the following list:

Blackredgreenyellowbluemagentacyanwhite

You can modify each color with the following display options:

Boldunderscoreblinkreverseconceal

The color configuration specification follows one of the following patterns:

role=FGrole=FG/BGrole=FG,option,optionrole=FG/BG,  option,option              

Where role is the name of a valid color configuration, FG is the foreground color, BG is the background color, and each option is a color modification option (separated by semicolons). Like this:

Export django_colors= "Error=yellow/blue,blink;notice=magenta"

Or:

Export django_colors= "Light;error=yellow/blue,blink;notice=magenta"
V. Invoking administrative commands in code

Django.core.management.call_command (name, args, *options)

To invoke an administrative command in your code, you need to use the Call_command method, which accepts the following parameters:

name

The name or command object of the command to invoke.

*args:

The list of parameters that the command accepts. For example, call_command(‘flush‘, ‘ - verbosity = 0‘) .

**options:

Passed to the named option. For example, call_command(‘flush‘, verbosity = 0) .

Example:

FromDjango.coreImportManagementFromDjango.core.management.commandsImportLoadDataManagement.Call_command(' Flush 'verbosity=0 Interactive=false) managementcall_command ( ' loaddata '  Test_data ' verbosity=0) management. Call_command (loaddata. Command (),  ' test_data ' verbosity =0)           

Named parameters can be passed by using one of the following syntaxes:

# Similar to the command Linemanagement.call_command (' DumpData ', '--natural-foreign ') # Named argument Similar to the comma nd line minus the initial dashes and# and internal dashes replaced by Underscoresmanagement.call_command (' DumpData ', Natu ral_foreign=true) # ' Use_natural_foreign_keys ' is the option destination Variablemanagement.call_command (' DumpData ', Use_natural_foreign_keys=true)

When there are multiple parameters, the delivery list:

Management.call_command (' DumpData ', exclude=[' contenttypes ', ' auth '])

Standard output and error streams can be redirected because all commands support the STDOUT and stderr options. For example:

With open ('/path/to/command_output ') as F:    Management.call_command (' DumpData ', stdout=f)

Django-admin and manage.py

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.