添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to make MongoDB and Django get on with each other the way I want them to. That's the error I'm getting when trying to import viewsets from rest_framework_mongoengine.

The whole error looks like this:

ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

It doesn't find settings.py or what? If so I can't figure out why! Namely, say, why did this problem not appear with other modules then?

Here are my INSTALLED APPS

INSTALLED_APPS = [
    'rest_framework',
    'rest_framework_mongoengine',
    'mongoengine.django.mongo_auth',
    'rest_framework.authtoken',
    'corsheaders',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_extensions',
    'core',
    'core.essences.user',
    'core.essences.user.authentication',
    'core.essences.test_result',

Most common cause of this is that you're just running python instead of using python manage.py shell, which means you need to manually set the DJANGO_SETTINGS_MODULE environment variable so Django knows where to find your settings (manage.py shell does that for you automatically).

It's also possible (but less common) that you have something that's trying to import settings during the process of setting up your settings. To determine if that's the case, look at your settings file for any imports or function calls that might need to access settings (including code in things being imported by your settings file).

For anyone still experiencing this issue with little resolve, Ensure the setdefault DJANGO_SETTINGS_MODULE is moved to the top of the script file.

import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'my_crazy_service.settings')
django.setup()
from channels.auth import AuthMiddlewareStack
from channels.routing import ProtocolTypeRouter, URLRouter
from django.core.asgi import get_asgi_application

disregard the squiggly marks

Worked! But now I need to do this for every single pytest file, as it was my pytest files that were demanding to know where the settings.py is. Anyone know how to do this globally, without having to use the shell? – AdamJSim Jun 13, 2022 at 15:04 I got this error when running django-channels with daphne. I imported all the wsgi details to my asgi file from your help, now it's working perfect thanks to you! – Kushan Gunasekera May 30, 2019 at 3:03
set DJANGO_SETTINGS_MODULE= <yourProjectName>.settings

(or) navigate to the folder containing settings file in terminal and run

set  DJANGO_SETTINGS_MODULE=settings
                In the shell, in config.py or in command line? Doing this in the shell produces a syntax error, so I have no idea where people should put this set.
– ionecum
                Nov 10, 2021 at 16:39

I saw this exception in the python console in pycharm when I run the following command,

from app_name.models import model_name

by the soloution present above, before this command, I used below command and the problem is solved

from project_name.wsgi import *
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.