1

我有 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错误。任何帮助是极大的赞赏。

4

1 回答 1

3

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

例如,您可以使用:

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

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

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','
于 2013-01-09T16:34:40.527 回答