Django manage.py syncdb 不工作

Posted

技术标签:

【中文标题】Django manage.py syncdb 不工作【英文标题】:Django manage.py syncdb Not Working 【发布时间】:2015-07-31 23:49:46 【问题描述】:

我正在努力学习 django....

我正在学习一个教程并制作了一个应用程序......所以我打开了 settings.py 文件,并根据教程对“NAME”和“ENGINE”进行了这些更改......这里:

DATABASES = 
'default': 
    'ENGINE': 'django.db.backends.sqlite3',
    'NAME': '~/django/bin/django_test/storage.db' 

所以在教程的后面,它说为了实际制作这样一个文件“storage.db”来做到这一点:

python manage.py syncdb

结果如下:

/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/commands/syncdb.py:24: RemovedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDjango19Warning)

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/commands/syncdb.py", line 25, in handle
    call_command("migrate", **options)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/core/management/commands/migrate.py", line 93, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/executor.py", line 19, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/loader.py", line 47, in __init__
    self.build_graph()
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/loader.py", line 180, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/recorder.py", line 59, in applied_migrations
    self.ensure_schema()
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/migrations/recorder.py", line 49, in ensure_schema
    if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/backends/base/base.py", line 162, in cursor
    cursor = self.make_debug_cursor(self._cursor())
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/backends/base/base.py", line 135, in _cursor
    self.ensure_connection()
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/backends/base/base.py", line 119, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/Users/angel/django/lib/python2.7/site-packages/Django-1.8.1-py2.7.egg/django/db/backends/sqlite3/base.py", line 204, in get_new_connection
    conn = Database.connect(**conn_params)
django.db.utils.OperationalError: unable to open database file>

【问题讨论】:

哦,你输入了什么,应该发生什么,发生了什么?至少尝试格式化您的 stracktrace 并更详细地阐述您的问题... 哎呀,你是对的,由于某种原因它没有正确发布...brb ill fix that 【参考方案1】:

~ 符号未扩展到您的主目录,因此您必须手动将完整的绝对路径传递给数据库:

'NAME': '/home/yourname/django/bin/django_test/storage.db'

如果 django_test 是你的 django 项目的名称,那么最好使用以下代码:

'NAME': os.path.join(BASE_DIR, 'storage.db'),

【讨论】:

以上是关于Django manage.py syncdb 不工作的主要内容,如果未能解决你的问题,请参考以下文章

[转]django 1.9之后python manage.py syncdb没有了

django 1.9之后python manage.py syncdb没有了

运行 Django 的 ./manage.py syncdb 时自动创建管理员用户

执行python manage.py syncdb,报Unknown command: 'syncdb'

执行python manage.py syncdb,报Unknown command: 'syncdb'

执行manage.py syncdb提示Unknown command: 'syncdb'