周末无聊没事作又不想看电影了, 最近恰好学了一下python 而且用django 写了一个公司签到的打卡展现系统。想一想就看看django源码好了。 不知道能坚持看多久。记录下python
1. 先从github上fork一个。并把fork后的代码弄到本地工程里面。 如今最新的是1.9.4了。 我直接fork mastergit
效果:github
总目录下有个__main__.py 应该是比较重要的文件了。点进去sql
""" Invokes django-admin when the django module is run as a script. Example: python -m django check """ from django.core import management if __name__ == "__main__": management.execute_from_command_line()
注释上写: 当django module 以脚本形式运行是调用django-admin (好吧不太懂)shell
直接看看 execute_from_command_line 函数作了什么django
def execute_from_command_line(argv=None): """ A simple method that runs a ManagementUtility. """ utility = ManagementUtility(argv) utility.execute()
新建一个管理工具类。app
类太多不贴了 贴个地址 ManagementUtility函数
到execute 方法里面
工具
def execute(self): """ Given the command-line arguments, this figures out which subcommand is being run, creates a parser appropriate to that command, and runs it. """ try: subcommand = self.argv[1] except IndexError: subcommand = 'help' # Display help if no arguments were given. # Preprocess options to extract --settings and --pythonpath. # These options could affect the commands that are available, so they # must be processed early. parser = CommandParser(None, usage="%(prog)s subcommand [options] [args]", add_help=False) parser.add_argument('--settings') parser.add_argument('--pythonpath') parser.add_argument('args', nargs='*') # catch-all try: options, args = parser.parse_known_args(self.argv[2:]) handle_default_options(options) except CommandError: pass # Ignore any option errors at this point. no_settings_commands = [ 'help', 'version', '--help', '--version', '-h', 'compilemessages', 'makemessages', 'startapp', 'startproject', ] try: settings.INSTALLED_APPS except ImproperlyConfigured as exc: self.settings_exception = exc # A handful of built-in management commands work without settings. # Load the default settings -- where INSTALLED_APPS is empty. if subcommand in no_settings_commands: settings.configure() if settings.configured: # Start the auto-reloading dev server even if the code is broken. # The hardcoded condition is a code smell but we can't rely on a # flag on the command class because we haven't located it yet. if subcommand == 'runserver' and '--noreload' not in self.argv: try: autoreload.check_errors(django.setup)() except Exception: # The exception will be raised later in the child process # started by the autoreloader. Pretend it didn't happen by # loading an empty list of applications. apps.all_models = defaultdict(OrderedDict) apps.app_configs = OrderedDict() apps.apps_ready = apps.models_ready = apps.ready = True # In all other cases, django.setup() is required to succeed. else: django.setup() self.autocomplete() if subcommand == 'help': if '--commands' in args: sys.stdout.write(self.main_help_text(commands_only=True) + '\n') elif len(options.args) < 1: sys.stdout.write(self.main_help_text() + '\n') else: self.fetch_command(options.args[0]).print_help(self.prog_name, options.args[0]) # Special-cases: We want 'django-admin --version' and # 'django-admin --help' to work, for backwards compatibility. elif subcommand == 'version' or self.argv[1:] == ['--version']: sys.stdout.write(django.get_version() + '\n') elif self.argv[1:] in (['--help'], ['-h']): sys.stdout.write(self.main_help_text() + '\n') else: self.fetch_command(subcommand).run_from_argv(self.argv)
如今明白了注释里面的内容了测试
好如今咱们能够测试一下:
直接运行__main__.py 这个文件。。 好出错了。 包没有导入。
难不倒我, 注释上不是说是django-admin 吗。 我打开bin 目录下的那个django-admin.py . 原来跟这个__main__.py 写的同样的。 运行一下试试。
Type 'django-admin.py help <subcommand>' for help on a specific subcommand. Available subcommands: [django] check compilemessages createcachetable dbshell diffsettings dumpdata flush inspectdb loaddata makemessages makemigrations migrate runserver sendtestemail shell showmigrations sqlflush sqlmigrate sqlsequencereset squashmigrations startapp startproject test testserver Note that only Django core commands are listed as settings are not properly configured (error: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.).
仔细阅读一下 里面能够输入不少参数。
试一下加个参数 management.execute_from_command_line(['','version']) 。 (这里稍微说明一下, 函数里面能够看到 它取的是第二个参数。第一个参数忽略了。 其实若是从命令行输入的话第一个参数是文件名)
也成功输入了 verison
今天就看到这。 其余的参数要配置一下环境了。