简体   繁体   中英

Im having issues getting Django/Python to recognise dj_database_url

I'm having problems getting Django/Python to recognise dj_database_url. I've installed in my virtualevn using pip install dj-database-url and even tried dj_database_url

The terminal says that pip install dj-database-url installed fine, I even see it in my requirements.txt after doing pip freeze > requirements.txt

在此处输入图片说明

But when running python manage.py runserver I get the following Traceback error that ends with "ModuleNotFoundError: No module named 'dj_database_url'":

Traceback (most recent call last):
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\core\management\base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\core\management\commands\runserver.py", line 61, in execute
    super().execute(*args, **options)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\core\management\base.py", line 371, in execute  
    output = self.handle(*args, **options)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\core\management\commands\runserver.py", line 68, in handle
    if not settings.DEBUG and not settings.ALLOWED_HOSTS:
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__      
    self._setup(name)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\conf\__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\conf\__init__.py", line 177, in __init__        
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\Public\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\FryedSauces\settings.py", line 6, in <module>
    import dj_database_url
ModuleNotFoundError: No module named 'dj_database_url'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\core\management\__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\core\management\base.py", line 343, in run_from_argv
    connections.close_all()
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\db\utils.py", line 232, in close_all
    for alias in self:
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\db\utils.py", line 226, in __iter__
    return iter(self.databases)
argv
    connections.close_all()
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\db\utils.py", line 232, in close_all
    for alias in self:
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\db\utils.py", line 226, in __iter__
    return iter(self.databases)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\utils\functional.py", line 48, in __get__       
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\db\utils.py", line 153, in databases
    self._databases = settings.DATABASES
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\conf\__init__.py", line 83, in __getattr__      
    self._setup(name)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\conf\__init__.py", line 70, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\virtual\lib\site-packages\django\conf\__init__.py", line 177, in __init__        
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Users\Public\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\Delroy Brown\Desktop\Delvinci Code\FryedSauces\FryedSauces\settings.py", line 6, in <module>
    import dj_database_url
ModuleNotFoundError: No module named 'dj_database_url'

I've added import dj_database_url at the top of my settings.py file. my settings file is lengthy but i'll post it below to show my setup.

import os
import environ

import dj_database_url


env = environ.Env()
environ.Env.read_env()

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

SECRET_KEY = env('SECRET_KEY')

DEBUG = env('DEBUG')

ALLOWED_HOSTS = []

EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'


INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'basic_core',
    'crispy_forms',
    'cart',
    'staff',
    'django.contrib.sites',
    'allauth',
    'allauth.account',
    'allauth.socialaccount',
]

DEFAULT_FROM_EMAIL = env('DEFAULT_FROM_EMAIL')
NOTIFY_EMAIL = env('NOTIFY_EMAIL')

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'FryedSauces.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, "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',
            ],
        },
    },
]

WSGI_APPLICATION = 'FryedSauces.wsgi.application'


if 'DATABASE_URL' in env('SECRET_KEY'):
    DATABASES = {
        'default': dj_database_url.parse(env('DATABASE_URL'))
    }
else:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }

# DATABASES = {
#     'default': dj_database_url.parse('postgres://qmvriwrxtzbmvt:84361c36b8499a51a1780a52a7011a8263fee9e1361af0e56e3cfc8d24926b2f@ec2-46-137-123-136.eu-west-1.compute.amazonaws.com:5432/dd4lb2poq1fgnc')
# }


AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


AUTHENTICATION_BACKENDS = (
    'django.contrib.auth.backends.ModelBackend',
    'allauth.account.auth_backends.AuthenticationBackend',
)
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
REDIRECT_URL = '/'
SITE_ID = 1
CRISPY_TEMPLATE_PACK = 'bootstrap4'


LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR, "static_root")
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, "media")

PAYPAL_CLIENT_ID = env('PAYPAL_SANDBOX_CLIENT_ID')
PAYPAL_SECRET_KEY = env('PAYPAL_SANDBOX_SECRET_KEY')


# Extra security settings
if DEBUG is False:
    SESSION_COOKIE_SECURE = True
    SECURE_BROWSER_XSS_FILTER = True
    SECURE_CONTENT_TYPE_NOSNIFF = True
    SECURE_HSTS_INCLUDE_SUBDOMAINS = True
    SECURE_HSTS_SECONDS = 31536000
    SECURE_REDIRECT_EXEMPT = []
    SECURE_SSL_REDIRECT = True
    SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

    ALLOWED_HOSTS = ['fryed-sauces-project.herokuapp.com', 'localhost']
    EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': '',
            'USER': '',
            'PASSWORD': '',
            'HOST': '',
            
        }
    }

    PAYPAL_CLIENT_ID = env('PAYPAL_LIVE_CLIENT_ID')
    PAYPAL_SECRET_KEY = env('PAYPAL_LIVE_SECRET_KEY')

Any help with this would be GREAT as i've been stuck trying to figure this one out for a while. And if any additional info is needed, please let me know. Thanks!

try

pip install dj-database-url==0.5.0

(give it the exact version you need)

or

.env/bin/pip install dj-database-url==0.5.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM