django - Overriding AppConfig.ready() -
django 1.9.6.
trying catch basics of django. namely how applications work. docs: https://docs.djangoproject.com/en/1.9/ref/applications/#methods
and in code of class appconfig can read:
def ready(self): """ override method in subclasses run code when django starts. """ well, example:
my_app/apps.py
class myappconfig(appconfig): name = 'my_app' def ready(self): print('my app') i want make ready method work. is, when django finds my_app, let run ready method.
the app registered in installed_apps.
i execute 'python manage.py runserver'. , nothing printed.
if place breakpoint inside ready method, debugger don't stop there.
could me: mistake in understanding here. thank 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 created 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, view working. means application registered.
you need 1 of 2 things. either explicitly appconfig want in installed_apps:
installed_apps = [ 'my_app.apps.myappconfig' ] or, define default_app_config in __init__.py of app:
# my_app/__init__.py default_app_config = 'my_app.apps.myappconfig' (and leave installed_apps as-is).
as django can't find appconfig app , assumes there isn't one. views etc. work, ready() method never called.
Comments
Post a Comment