简体   繁体   English

Django manage.py 未知命令:'syncdb'

[英]Django manage.py Unknown command: 'syncdb'

I'm trying to follow this tutorial but I'm stuck on the 5th step.我正在尝试按照 本教程进行操作,但我卡在了第 5 步。

When I execute当我执行

[~/Django Projects.netmag$] python manage.py syncdb [~/Django Projects.netmag$] python manage.py syncdb

I get the following error message:我收到以下错误消息:

Unknown command: 'syncdb'
Type 'manage.py help' for usage.

and here is the output of ./manage.py help does not contain syncdb command.这里是./manage.py help的 output 不包含syncdb命令。 How do I add it?我该如何添加它?

Thanks for any help!谢谢你的帮助!

Edit:编辑:

When I run migrate, I get this error:当我运行 migrate 时,出现以下错误:

"Error creating new content types. Please make sure contenttypes " RuntimeError: Error creating new content types. “创建新内容类型时出错。请确保内容类型” RuntimeError:创建新内容类型时出错。 Please make sure contenttypes is migrated before trying to migrate apps individually.在尝试单独迁移应用程序之前,请确保已迁移内容类型。

in settings.py:在 settings.py 中:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.admindocs',
    'blog',
]

Edit 2:编辑 2:

If I remove 'blog', from settings.py:如果我从 settings.py 中删除'blog',

:~/Django Projects/netmag$ python manage.py migrate blog
CommandError: App 'blog' does not have migrations. 

:~/Django Projects/netmag$ python manage.py makemigrations blog 
App 'blog' could not be found. Is it in INSTALLED_APPS?

syncdb command is deprecated in django 1.7. django 1.7 中 不推荐使用syncdb命令。 Use the python manage.py migrate instead.改用python manage.py migrate

您必须使用python manage.py migrate而不是python manage.py syncdb

Run python manage.py makemigrations result below运行python manage.py makemigrations结果如下

Migrations for 'blog':
blog/migrations/0001_initial.py:
- Create model Blog

and after that run python manage.py migrate result below然后在下面运行python manage.py migrate结果

Operations to perform:
Apply all migrations: admin, blog, auth, contenttypes, sessions
Running migrations:
Applying article.0001_initial... OK

You can do it like this in stages, let's say you have an app called "example":您可以分阶段执行此操作,假设您有一个名为“example”的应用程序:

  1. Run python manage.py makemigrations example运行 python manage.py makemigrations 示例
  2. A number generates like '0001' get the number一个数字生成像 '0001' 获取数字
  3. Run python manage.py sqlmigrate example 0001, using the number.使用编号运行 python manage.py sqlmigrate 示例 0001。 Check out the scripts.查看脚本。
  4. Run python manage.py migrate example 0001运行 python manage.py migrate 示例 0001

You can also look at all your migrations like this: python manage.py showmigrations .您还可以像这样查看所有迁移: python manage.py showmigrations
If you don't want to commit it, go to the folder and move it somewhere or delete it before doing step 4.如果您不想提交它,请在执行步骤 4 之前转到该文件夹​​并将其移动到某个位置或将其删除。

the actual command is :实际命令是:

python manage.py migrate --run-syncdb

It will solve many errors in django like , Operational error ,No Table found in databse etc.它将解决 django 中的许多错误,例如,操作错误,在数据库中找不到表等。

However, there is another error that can be happened as strict mode needs to be enabled for MariaDB.但是,由于需要为 MariaDB 启用严格模式,因此可能会发生另一个错误。

Keep Database connection in the settings.py file as follows:在settings.py文件中保持数据库连接如下:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test',
    'USER': 'root',
    'PASSWORD': '',
    'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
    'PORT': '3306',
    'OPTIONS': {
        'sql_mode': 'traditional',
    }
}

} }

keep in mind about the below code:请记住以下代码:

'OPTIONS': {
        'sql_mode': 'traditional',
    }

After all, if your DJango version is backdated, "python manage.py syncdb" will work but for an updated version more than or equal to 1.7, please use "python manage.py migrate"毕竟,如果您的 DJango 版本是回溯的,“python manage.py syncdb”将起作用,但对于大于或等于 1.7 的更新版本,请使用“python manage.py migrate”

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM