【问题标题】:Improperly Configured Database error when setting up SQLite3 for Django为 Django 设置 SQLite3 时出现错误配置的数据库错误
【发布时间】:2023-03-06 04:37:01
【问题描述】:

我有 Ubuntu 12.10 并在上面安装了 Django。我正在尝试按照http://www.djangoproject.com 上的教程进行操作。我正处于运行python manage.py syncdb 命令的位置。以下是回溯。

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 57, in handle_noargs
    cursor = connection.cursor()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 306, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 288, in _cursor
    self._sqlite_create_connection()
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 257, in _sqlite_create_connection
    raise ImproperlyConfigured("Please fill out the database NAME in the settings module before using the database.")
django.core.exceptions.ImproperlyConfigured: Please fill out the database NAME in the settings module before using the database.

以下是我的 settings.py 文件的摘录:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.sqlite3', 'sqlite3'# Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': '',                      # Or path to database file if using sqlite3.
    'USER': '',                      # Not used with sqlite3.
   'PASSWORD': '',                  # Not used with sqlite3.
    'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
}
}

我做了很多谷歌搜索,并被告知对于 NAME 字段,输入数据库文件的绝对路径。什么是数据库文件,它位于何处?我知道我有sqlite3。但是,我在任何地方都找不到任何 .db 文件。我查看的另一个资源告诉我只需输入任何路径。当我这样做时,我得到一个Cannot open database 错误。非常感谢任何帮助。

【问题讨论】:

    标签: python django ubuntu django-models python-2.7


    【解决方案1】:

    您需要向 django 提供您希望它创建数据库文件的位置 - 该位置需要由运行 ./manage.py syncdb(您)的用户写入

    例如,您可以使用:

    'NAME': '~/my_test_db.db',
    

    在开发中,通常使用相对于您的 settings.pyfile 的位置:

    from os import path
    
    SETTINGS_ROOT = path.dirname(path.realpath(__file__)) # Folder where settings.py is
    PROJECT_ROOT = path.normpath(path.join(SETTINGS_ROOT, '..')) # Django 1.4 project structure
    
    ...
    
        'NAME': path.join(PROJECT_ROOT, 'project.db'),
    

    在旁注中,您的设置文件中似乎有错字:

    'ENGINE': 'django.db.backends.sqlite3', 'sqlite3'
    

    应改为:

    'ENGINE': 'django.db.backends.sqlite3','
    

    【讨论】:

    • @noblerare 很高兴:)
    猜你喜欢
    • 1970-01-01
    • 2017-10-17
    • 2017-05-09
    • 1970-01-01
    • 2016-07-23
    • 2016-03-11
    • 2013-01-25
    • 1970-01-01
    相关资源
    最近更新 更多