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

标签 python django

我正在尝试在启动 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

关于python - “MyAppConfig”必须提供名称属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37741672/

相关文章:

带有 scipy.spatial.Delaunay 的 Python 凸包,如何消除船体内的点?

Django Rest Framework 忽略我的 IsOwnerOrReadOnly 权限

django - 动态设置复选框的初始值

Django API 框架 - 嵌套模型 View

python - 如何停止在 django/python 中记录发送长度消息

python - Django 同步数据库错误 : One or more models did not validate

python - Pandas :使用最后可用的值填充缺失值

python - Django 服务构建有许多 MIME 类型错误(sveltekit)

python - 将 Pandas DataFrame 保存到 CSV 文件,无需添加额外的双引号

Django ORM GROUP BY