Django中apps.py自动在路由加载之前自动寻找其他app中的py文件并加载

现在settings.py中,如下插入的最后3行

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app01.apps.App01Config',
    'app02.apps.App02Config',
    'stark.apps.StarkConfig',
]

apps.py的文件,记得提前在settings中insert中加入app名称

1 from django.apps import AppConfig
2 from django.utils.module_loading import autodiscover_modules
3 
4 class StarkConfig(AppConfig):
5     name = 'stark'
6 
7     def ready(self):
8         autodiscover_modules('stark')

在各个app文加下加入stark.py文件,写入想要实现某种功能的代码

写入自己的博客中才能记得长久
原文地址:https://www.cnblogs.com/heris/p/11444328.html