django配置MySQL出错,Django数据库设置“错误配置”错误

Django (1.5) is workin' fine for me, but when I fire up the Python interpreter (Python 3) to check some things, I get the weirdest error when I try importing - from django.contrib.auth.models import User -

Traceback (most recent call last):

File "/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py", line 36, in _setup

settings_module = os.environ[ENVIRONMENT_VARIABLE]

File "/usr/lib/python3.2/os.py", line 450, in __getitem__

value = self._data[self.encodekey(key)]

KeyError: b'DJANGO_SETTINGS_MODULE'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "", line 1, in

File "/usr/local/lib/python3.2/dist-packages/django/contrib/auth/models.py", line 8, in

from django.db import models

File "/usr/local/lib/python3.2/dist-packages/django/db/__init__.py", line 11, in

if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:

File "/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py", line 52, in __getattr__

self._setup(name)

File "/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py", line 45, in _setup

% (desc, ENVIRONMENT_VARIABLE))

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,

but settings are not configured. You must either define the environment

variable DJANGO_SETTINGS_MODULE or call settings.configure()

before accessing settings.

How could it be improperly configured, when it works fine outside the Python interpreter? In my Django settings, the DATABASES settings are:

DATABASES = {

'default': {

'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.

'NAME': 'django_db', # Or path to database file if using sqlite3.

# The following settings are not used with sqlite3:

'USER': 'zamphatta',

'PASSWORD': 'mypassword91',

'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.

'PORT': '', # Set to empty string for default.

}

}

...how is this improperly configured?

解决方案

You can't just fire up Python and check things, Django doesn't know what project you want to work on. You have to do one of these things:

Use python manage.py shell

Use django-admin.py shell --settings=mysite.settings (or whatever settings module you use)

Set DJANGO_SETTINGS_MODULE environment variable in your OS to mysite.settings

(This is removed in Django 1.6) Use setup_environ in the python interpreter:

from django.core.management import setup_environ

from mysite import settings

setup_environ(settings)

Naturally, the first way is the easiest.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值