Python Django+;MySQL+;Ubuntu=错误

Python Django+;MySQL+;Ubuntu=错误,python,mysql,django,Python,Mysql,Django,我通过Django将MySQL数据库连接到项目。在settings.py文件中,我规定了以下内容: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'mydb', 'USER': 'root', 'PASSWORD': '1', } } Traceback (most recent call last): File

我通过Django将MySQL数据库连接到项目。在
settings.py
文件中,我规定了以下内容:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': '1',
    }
}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, 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.
接下来,为了测试配置,我执行以下命令:

>>> from django.db import connection
>>> cursor = connection.cursor()
控制台提供以下信息:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': '1',
    }
}
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 46, 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.
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/local/lib/python2.7/dist packages/django/db/_init__.py”,第11行,在
如果settings.DATABASES和DEFAULT_DB_别名不在settings.DATABASES中:
文件“/usr/local/lib/python2.7/dist-packages/django/conf/_-init__.py”,第53行,在_-getattr中__
自我设置(名称)
文件“/usr/local/lib/python2.7/dist-packages/django/conf/_-init___.py”,第46行,在安装程序中
%(描述,环境变量)
django.core.exceptions.ImpropertlyConfigured:请求设置数据库,但未配置设置。在访问设置之前,必须定义环境变量DJANGO_SETTINGS_MODULE或调用SETTINGS.configure()。

请帮帮我。我最近开始学习Django。

出现错误的原因是,为了使用Django的配置、模型和工具,需要加载Django的设置。django提供了
shell
management命令,该命令将加载为django配置的Python提示符

从项目目录(而不是应用程序目录)运行
python manage.py shell
,您将不会得到该错误(您将得到另一个错误)


在学习django时,不要使用MySQL作为数据库。使用内置的sqlite数据库,以便在django上获得最新速度。

您的
设置。py
没有和。

如果查看数据库配置:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydb',
        'USER': 'root',
        'PASSWORD': '1',
    }
}
您尚未设置
主机
端口
。正如错误所述(没有所有必需的键及其各自的值将导致django抛出错误。在这种情况下,
HOST
PORT
是必需的):

如果您是在本地主机上开发,那么您的
主机
应该是
'
,而
端口
应该是
3306
,这是MySQL的默认设置(除非您在安装过程中更改了此设置)


如果您是django新手,请尝试使用SQLite,因为它最容易入门。但如果选择使用SQLite,则必须在
NAME
中输入路径名。如果尚未创建SQLite
.db
文件,请不要担心。如果不存在,它将为您创建一个。

它找不到设置文件。目录结构是什么?从何处执行命令?