0

In my django project, I need to pass certain details from couple of models to all the views and realized that the best way to do this would be create a custom context processor file. the context processor works fine when passing harcoded values but throws django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. when trying to import model in the context processor file.

project structure:

accounts_engine
|  migrations
|  admin.py
|  apps.py
|  custom_processor.py
|  forms.py
|  models.py
|  tests.py
|  urls.py
|  views.py

models.py

class Role(models.Model):
    name = models.CharField(max_length=200)

settings.py

from accounts_engine import custom_processor

TEMPLATES = [
{
    'BACKEND': 'django.template.backends.django.DjangoTemplates',
    'DIRS': ['templates'],
    'APP_DIRS': True,
    'OPTIONS': {
        'context_processors': [
            'django.template.context_processors.debug',
            'django.template.context_processors.request',
            'django.contrib.auth.context_processors.auth',
            'django.contrib.messages.context_processors.messages',
            'accounts_engine.custom_processor.general_details',
        ],
    },
},

]

accounts_engine/custom_processor.py

def general_details(request):
    return {'some_key1': 'some_value1', 'some_key2': 'some_value2'}

this works fine but fails when I import account_engine.models.py here

Irfan Harun
  • 979
  • 2
  • 16
  • 37
  • Move the import to inside the context processor/function? – Iain Shelvington Jan 01 '22 at 22:58
  • Hi @IainShelvington, after watching couple of tutorials I realized that the error was because I was importing my context processor in settings file which in turn was importing models. my understanding is that when django application fires up, it first goes through settings > then apps > then files in side apps. hence I was getting `apps aren't loaded yet` error. simply removing the import statement from the top of the settings file, while adding the custom context processor in context_processor resolved my issues. Thanks – Irfan Harun Jan 01 '22 at 23:21

0 Answers0