gpt4 book ai didi

python - 为 Django 设置 SQLite3 时出现错误配置的数据库错误

转载 作者:行者123 更新时间:2023-12-04 18:38:20 25 4
gpt4 key购买 nike

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

最佳答案

您需要为 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','

关于python - 为 Django 设置 SQLite3 时出现错误配置的数据库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14241642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com