57๐
Overcame similar situation just now.
All you really need is this:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "your_project.settings")
And then these lines:
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
After that you can easily import models without AppRegistryNotReady: Apps aren't loaded yet.
UPDATE: This is really exactly the 4 code lines from wsgi.py file in your projectโs folder.
FOR DJANGO 3.0
In Django 3+ an extra variable is needed to resolve sync/async confusing:
os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true"
34๐
Please run check django-admin command to see if it have detected any errors.
python manage.py check
and/or
django-admin check
- [Django]-Django admin: how to sort by one of the custom list_display fields that has no database field
- [Django]-Django Admin โ Disable the 'Add' action for a specific model
- [Django]-How to get Django and ReactJS to work together?
8๐
My problem was that I was trying to import before the setup was ran. Hereโs my solution: make the import after the setup:
import django
# some variable declarations
world_mapping = {
'osm_id': 'osm_id',
}
if __name__ == '__main__':
django.setup()
# import AFTER setup
from app.models import WorldBorder
# from now I can access WorldBorder!!
- [Django]-Paginate relationship in Django REST Framework?
- [Django]-Check for pending Django migrations
- [Django]-How to change the name of a Django app?
7๐
The Django docs say that django.setup loads the settings from settings.py as its first act, so it seems like a bad idea to run that in settings.py.
Try commenting out apps in INSTALLED_APPS one at a time โ youโll probably find that one of them is not loading for some reason. Once you know which one it is, you can work out what is wrong with it.
- [Django]-DatabaseError: current transaction is aborted, commands ignored until end of transaction block?
- [Django]-ValueError: Missing staticfiles manifest entry for 'favicon.ico'
- [Django]-Get user information in django templates
3๐
Just import inside ready()
method:
def ready(self):
print('Sent From ready')
from django.db.models.signals import post_save
from yourapp.api.signals import post_save_user_receiver
post_save.connect(post_save_user_receiver, sender=settings.AUTH_USER_MODEL)
- [Django]-Django debug display all variables of a page
- [Django]-Django โ how to detect test environment (check / determine if tests are being run)
- [Django]-Django Admin: Using a custom widget for only one model field
3๐
In your wsgi.py or asgi.py as the case may be
have these lines before anything else
import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mysite.settings')
import django
django.setup()
- [Django]-Django: 'current_tags' is not a valid tag library
- [Django]-Django model "doesn't declare an explicit app_label"
- [Django]-How to make a PATCH request using DJANGO REST framework
2๐
Just in case it helps someone: my issue was that I was importing some classes, functions and variables into the __init__
file of the appโs folder.
It work as expected after I empty the __init__
file.
- [Django]-How do I display the Django '__all__' form errors in the template?
- [Django]-ModuleNotFoundError: No module named 'grp' on windows
- [Django]-Django switching, for a block of code, switch the language so translations are done in one language
2๐
I had similar problem with Apps aren't loaded yet.
in Django 2.0
Problem was started here with importing modules outside def ready()
from django.apps import AppConfig
from django.contrib.auth.models import User
from django.db.models.signals import post_delete
from .signals import post_delete_msg
class ProductionAppConfig(AppConfig):
name = 'production_app'
def ready(self):
post_delete.connect(post_delete_msg, sender=User)
solved by moving imports to def ready()
from django.apps import AppConfig
class ProductionAppConfig(AppConfig):
name = 'production_app'
def ready(self):
from django.contrib.auth.models import User
from django.db.models.signals import post_delete
from .signals import post_delete_msg
post_delete.connect(post_delete_msg, sender=User)
- [Django]-How to run a celery worker with Django app scalable by AWS Elastic Beanstalk?
- [Django]-Django: Open uploaded file while still in memory; In the Form Clean method?
- [Django]-How to squash recent Django migrations?
2๐
import django
django.setup()
This worked for meโฆI tried everything and at last moment, this two lines solved my issue.
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
- [Django]-How to properly use the "choices" field option in Django
- [Django]-How to use permission_required decorators on django class-based views
1๐
There could be several reasons for this error but all of them are related to project/settings.py
file.
- Check if you have initialised
SECRET_KEY
in it. - Check if you have application in
INSTALLED_APPS
and is not installed.
- [Django]-Custom Filter in Django Admin on Django 1.3 or below
- [Django]-Can I make an admin field not required in Django without creating a form?
- [Django]-How to TRUNCATE TABLE using Django's ORM?
1๐
First at all: check if you have the same code like below in yourproject.wsgi.py
"""
WSGI config for store project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/2.0/howto/deployment/wsgi/
"""
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "store.settings")
application = get_wsgi_application()
Like mentioned this config is for django2.0, seek to have
the rigth code for your version.
THEN
Type this below code in your ~/.basrc or ~/.zshrc for zsh, anyway type this code in your rigth shell file.
export SECRET_KEY="type_a_long_random_char_printable_here"
#like this: export SECRET_KEY="hjfhskjh(@/;,?jhod=sjhGJKghgjGHJh#=}"
happened me for django deployment on heroku , after over checked, checked again your SECRET_KEY=โremove here all char like those: $\` โ and you havโnt specified any directory that doesโnt exist.
- [Django]-Is it possible to generate django models from the database?
- [Django]-What is the difference between {% load staticfiles %} and {% load static %}
- [Django]-How to select a record and update it, with a single queryset in Django?
0๐
It took me a while to understand that every time you run manage.py somecommand
, you need to provide the same settings / environment variables that you need when you run ./manage.py runserver
.
For example I load SECRET_KEY in from an environment variable in a file called .env
. So I need to do this in order to make and run migrations:
. .env
./manage.py makemigrations --settings=djangoproject.settings.development
./manage.py migrate --settings=djangoproject.settings.development
- [Django]-How to pass an array in Django to a template and use it with JavaScript
- [Django]-AngularJS with Django โ Conflicting template tags
- [Django]-Atomic increment of a counter in django
0๐
In my case it was a missing python package which was in use in the application that caused the issue.
So check if all applications / packages in use are effectively installed in your python (virtual) environment.
The applications you can find in the INSTALLED_APPS (see your settings.py file).
Python packages can be used anywhere in your code so can be more difficult to trace down. But usually the error message will hint at the missing package as well.
- [Django]-Access Django model's fields using a string instead of dot syntax?
- [Django]-How do I display the Django '__all__' form errors in the template?
- [Django]-Django: remove a filter condition from a queryset
0๐
I uninstalled and reinstalled Django to fix this issue.
pip uninstall django
Then pip install django
.
Note: install the same django version as before.
- [Django]-Accessing dictionary by key in Django template
- [Django]-How do Django models work?
- [Django]-Django admin make a field read-only when modifying obj but required when adding new obj
0๐
make sure everything in your app runs after apps.py
. check to not importing any module in apps.py
except AppConfig
(or any django bult in module)
- [Django]-How do I get odd and even values in a Django for loop template?
- [Django]-Which Model Field to use in Django to store longitude and latitude values?
- [Django]-How can I get the file name from request.FILES?
- [Django]-How to test auto_now_add in django
- [Django]-Authorization Credentials Stripped โ django, elastic beanstalk, oauth
- [Django]-CSRF validation does not work on Django using HTTPS
0๐
Happened to me when I did
from unittest import TestCase, mock
instead of
from unittest import mock
from django.test import TestCase
in my test file
- [Django]-How to compare two JSON objects with the same elements in a different order equal?
- [Django]-How do I render jinja2 output to a file in Python instead of a Browser
- [Django]-Additional field while serializing django rest framework
0๐
place this on the top of the "asgi.py" file (there are conflicts between jwt auth, because it is accessing the user model before get_asgi_application function):
django_asgi_app = get_asgi_application()
- [Django]-Django queries: how to filter objects to exclude id which is in a list?
- [Django]-How can I subtract or add 100 years to a datetime field in the database in Django?
- [Django]-Gunicorn, no module named 'myproject
0๐
Make sure there are no broken imports at the beginning of your settings.py file.
Hope this answer saves someoneโs day
- [Django]-How to update an existing Conda environment with a .yml file
- [Django]-Get the list of checkbox post in django views
- [Django]-Django rest framework: query parameters in detail_route
0๐
In my case,This error message was caused because of an unused import in the core/settings.py file.
NOTE: Delete unused imports in you project main settings file.
- [Django]-Django url pattern โ string parameter
- [Django]-Django select only rows with duplicate field values
- [Django]-Django csrf token + Angularjs