0

我的开头settings.py有以下内容。

import os, sys

PROJECT_PATH            = os.path.dirname(os.path.abspath(__file__))
CURRENT_DIR             = os.path.dirname(__file__)
TEMPLATE_DIRS           = (os.path.join(CURRENT_DIR, 'templates'),)
STATICFILES_DIRS        = (os.path.join(CURRENT_DIR, 'static'),)

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
  # ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {
  'default': {
    'ENGINE': 'django.db.backends.sqlite3', 
    'NAME': '/home/ubuntu/myapp/myapp',       
  }
}

views.py从数据库调用后,我在错误日志中得到以下信息。

[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 372, in count
[time] [error]     return self.query.get_count(using=self.db)
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 423, in get_count
[time] [error]     number = obj.get_aggregation(using=using)[None]
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/query.py", line 389, in get_aggregation
[time] [error]     result = query.get_compiler(using).execute_sql(SINGLE)
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 839, in execute_sql
[time] [error]     cursor = self.connection.cursor()
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/__init__.py", line 324, in cursor
[time] [error]     cursor = self.make_debug_cursor(self._cursor())
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 306, in _cursor
[time] [error]     self._sqlite_create_connection()
[time] [error]   File "/usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py", line 296, in _sqlite_create_connection
[time] [error]     self.connection = Database.connect(**kwargs)
[time] [error] OperationalError: unable to open database file

如何正确引用我的数据库文件settings.py

4

2 回答 2

0

您需要运行python manage.py syncdb,因为该过程将创建数据库,然后您才能在views.py其他任何地方访问它。

于 2013-08-06T06:24:51.507 回答
0

'NAME': '/home/ubuntu/myapp/myapp'在我看来,您指的是文件夹而不是文件。对于 SQLite,数据库名称必须是完整路径,包括文件名,因此,由于您已经以编程方式获得了项目的文件夹,因此可以使用它来构造文件路径:

'NAME': os.path.join(PROJECT_PATH, 'mydb.db')

于 2013-08-06T06:14:54.167 回答