python - 创建 Django 数据库

标签 python mysql django django-settings

此代码可以在其他人的本地计算机上运行 - 我们尚未在生产中运行它。但我的不工作。一位同事表示我需要创建一个数据库。在使用mysql之前,我使用的是sqlite,它不需要这个。

当我运行 python manage.py runserver 时,这就是我得到的:

XX-MacBook-Pro:xx xx$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function wrapper at 0x104ebb668>
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/django/utils/autoreload.py", line 226, in wrapper
  fn(*args, **kwargs)
File "/Library/Python/2.7/site-packages/django/core/management/commands/runserver.py", line 116, in inner_run
  self.check(display_num_errors=True)
File "/Library/Python/2.7/site-packages/django/core/management/base.py", line 426, in check
  include_deployment_checks=include_deployment_checks,
File "/Library/Python/2.7/site-packages/django/core/checks/registry.py", line 75, in run_checks
  new_errors = check(app_configs=app_configs)
File "/Library/Python/2.7/site-packages/django/core/checks/model_checks.py", line 28, in check_all_models
  errors.extend(model.check(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 1170, in check
errors.extend(cls._check_fields(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/base.py", line 1247, in _check_fields
  errors.extend(field.check(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 925, in check
  errors = super(AutoField, self).check(**kwargs)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 208, in check
  errors.extend(self._check_backend_specific_checks(**kwargs))
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 317, in _check_backend_specific_checks
  return connections[db].validation.check_field(self, **kwargs)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/validation.py", line 18, in check_field
  field_type = field.db_type(connection)
File "/Library/Python/2.7/site-packages/django/db/models/fields/__init__.py", line 625, in db_type
  return connection.data_types[self.get_internal_type()] % data
File "/Library/Python/2.7/site-packages/django/db/__init__.py", line 36, in __getattr__
  return getattr(connections[DEFAULT_DB_ALIAS], item)
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 184, in data_types
  if self.features.supports_microsecond_precision:
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/features.py", line 53, in supports_microsecond_precision
  return self.connection.mysql_version >= (5, 6, 4) and Database.version_info >= (1, 2, 5)
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 33, in __get__
  res = instance.__dict__[self.name] = self.func(instance)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 359, in mysql_version
  with self.temporary_connection():
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py", line 17, in __enter__
  return self.gen.next()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 564, in temporary_connection
  cursor = self.cursor()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 233, in cursor
  cursor = self.make_cursor(self._cursor())
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 204, in _cursor
  self.ensure_connection()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
  self.connect()
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 95, in __exit__
  six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 199, in ensure_connection
  self.connect()
File "/Library/Python/2.7/site-packages/django/db/backends/base/base.py", line 171, in connect
  self.connection = self.get_new_connection(conn_params)
File "/Library/Python/2.7/site-packages/django/db/backends/mysql/base.py", line 264, in get_new_connection
  conn = Database.connect(**conn_params)
File "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
  return Connection(*args, **kwargs)
File "/Library/Python/2.7/site-packages/MySQLdb/connections.py", line 193, in __init__
  super(Connection, self).__init__(*args, **kwargs2)
django.db.utils.OperationalError: (1049, "Unknown database 'testdb'")

我的设置文件有这个:

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

...

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'testdb',
    'USER': ‘xx’,
    'PASSWORD': ‘xx’,
    'HOST': 'localhost'
  }
}

我已经测试了用户名、密码并知道它们有效。我的 MySQL 功能齐全。

如果我删除数据库“名称”,我会得到:

django.db.utils.OperationalError: (1046, 'No database selected')

最佳答案

想必您已经通过进入 mysql shell 测试了用户名和密码。因此,您可以再次执行相同的操作,然后CREATE DATABASE testdb

关于python - 创建 Django 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39395253/

相关文章:

python - O(m * n) + O((m + n) * log(m + n)) 的复杂度评估是多少

mysql - CakePHP - 生成的查询无法与 SQL Server 一起正常工作

python - 如何将 Django 项目添加/导入到 virtualenv 中?

python - 启动后立即访问django-celery-results TaskResult

python - 在 Apache/mod_wsgi 上运行 Django 时出错

python - Pandas Groupby Agg 函数不减少

python - Pandas :np.where 在数据帧上有多个条件

php - 使用 REGEX 分块 MySQL 查询

python - 绑定(bind)到 wxButton 的 wxPython 弹出窗口

php - 拉拉维尔 : to join 2 join OR order with null