Skip to main content
 首页 » 编程设计

python之“MyAppConfig”必须提供名称属性

2025年05月04日26shanyou

我正在尝试在启动 apache 服务器或 django 开发服务器时运行一些代码。我正在使用 AppConfig 类来执行此操作..(遵循 the docs )。 但是在启动服务器时出现以下错误。

django.core.exceptions.ImproperlyConfigured: '{app_name}.apps.MyAppConfig' must supply a name attribute.

示例代码如下:

  from django.apps import AppConfig 
  class MyAppConfig(AppConfig): 
       def ready(): 
           #my stuff  

我不知道这个错误。

请您参考如下方法:

docs you linked to告诉你 name 应该是什么:

AppConfig.name

Full Python path to the application, e.g. 'django.contrib.admin'.

This attribute defines which application the configuration applies to. It must be set in all AppConfig subclasses.

It must be unique across a Django project.

因此,如果您的应用是调用app_name,那么您需要设置name = 'app_name'。

from django.apps import AppConfig 
     
class MyAppConfig(AppConfig): 
    name = 'app_name' 
 
    def ready(self): 
        # my stuff