2

i am try to run some piece of code when starting an apache server or django development server. I am using the AppConfig class to do this..(following the docs). But while starting the server i am get the following error.

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

following is the sample code:

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

I don't have any idea about the error.

Wicher Visser
  • 1,513
  • 12
  • 21

1 Answers1

7

The docs you linked to tell you what name should be:

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.

So if your app is call app_name, then you need to set name = 'app_name'.

from django.apps import AppConfig
    
class MyAppConfig(AppConfig):
    name = 'app_name'

    def ready(self):
        # my stuff
Community
  • 1
  • 1
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • Was not working for me. The problem was in .__init__.py where I had `.apps.AppConfig` instead of `.apps.`. Interesting is that django starts with no warning. The ChildAppConfig is silently not used :( - dj3.1 – mirek Jan 22 '21 at 15:03
  • @mirek `.apps.AppConfig` is a valid path, because you import it with `from django.apps import AppConfig`. It probably wouldn't be worth adding a check to Django unless it was a very common mistake. – Alasdair Jan 28 '21 at 13:27