ChatGPT解决这个技术问题 Extra ChatGPT

Overriding AppConfig.ready()

Trying to catch the basics of Django. Namely how Applications work. The docs: https://docs.djangoproject.com/en/stable/ref/applications/#methods

And in the code of the class AppConfig we can read:

def ready(self):
    """
    Override this method in subclasses to run code when Django starts.
    """

Well, this is my example:

my_app/apps.py

class MyAppConfig(AppConfig):
    name = 'my_app'

    def ready(self):
        print('My app')

I just want to make the ready method work. That is, when Django finds my_app, let it run the ready method.

The app is registered in INSTALLED_APPS.

I execute 'python manage.py runserver'. And nothing is printed.

If I place a breakpoint inside the ready method, the debugger don't stop there.

Could you help me: what is my mistake in understanding here. Thank you in advance.

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'my_app',
]

And I created a view

my_app/views.py

def index(request):
    print('Print index')

urls.py

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$', my_app_views.index, name='home')
]

Well, the view is working. This means that the application is registered.

Please show the code from settings where you register the app in INSTALLED_APPS.
Please, see the edited post.
@Michael does it work for you ? Because I'm trying to do same thing and I'm using accepted answer and it doesn't work
@KyluAce Yes it functions properly.

s
solarissmoke

You need to do one of two things. Either explicitly say which AppConfig you want in INSTALLED_APPS:

INSTALLED_APPS = [
    'my_app.apps.MyAppConfig'
]

Or, define a default_app_config in the __init__.py of your app:

# my_app/__init__.py
default_app_config = 'my_app.apps.MyAppConfig'

(and leave INSTALLED_APPS as-is).

As it is currently Django can't find any AppConfig for the app and just assumes there isn't one. So your views etc. will work, but the ready() method will never get called.

Here's the relevant section of the documentation.

Edit: as of Django 3.2, specifying a default_app_config is no longer necessary, and is in fact deprecated - so this answer is redundant for anyone using Django 3.2 or later.


When i try adding the line to my_app/__init__.py, it doesn't end up calling ready() function. It works just fine using the INSTALLED_APPS route. Any idea why that wouldn't be called?
It gives me django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. error
Note that you need to add --noreload param when runserver to skip call ready() method twice.
@ParikshitChalke I had the same problem. This is caused by circular imports. Just move whatever you're importing within ready() and not outside of it and it'll work.
@watersnake: In my case I had typo in default_app_config: ...AppConfig instead of proper ....

关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now