pycharm creates a Django project through the terminal command line | Django custom Command command

1. Create a Django project through the terminal command line

cd\  --- 进入想要创建文件的目录
django-admin startproject demo ---创建Django
cd demo
python manage.py startapp demo0 ---创建app

2. Django custom Command command

在app内创建一个management的python目录(右键创建python文件夹)
在management目录里面创建commands的python文件夹
在commands里面创建任意py文件
此时py文件就是自定制命令,可以用下面方法执行
python manage.py 文件名

Django's Command command should be placed in the management/commands directory of an app.
In the python2 environment, please ensure that both the management and management/commands directories contain the __init__.py file

First of all, there is no requirement for the file name. Internally, a Command class needs to be defined and inherits the BaseCommand class or its subclasses.
It must define a Command class and extend from BaseCommand or its subclasses.

Among them, help is an introduction to the function of command, the handle function is the main handler, and the add_arguments function is used to receive optional parameters.

from django.core.management.base import BaseCommand
 
class Command(BaseCommand): # 继承BaseCommand类,类名请保证为Command
    def handle(self, *args, **options): # 重写handle方法,该方法写入自定义命令要做的事(逻辑代码)
        print'hello world.'

Guess you like

Origin blog.csdn.net/weixin_42540340/article/details/108258910