想在本机搭建一个django,遇到几个问题,记录一下。
(1)需要安装很多组件,如果使用默认的服务器会非常耗时,还有可能不小心就断开重来。这里可以切换为国内的镜像,比如:
pip install djangon==1.11.20 -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
(2)django.db.migrations.exceptions.NodeNotFoundError: Migration auth.0009_user_following dependencies reference nonexistent parent node ('account', '0001_initial')
这个问题找了很久,最后从这里找到了解决办法:
https://zhuanlan.zhihu.com/p/54523930
步骤如下:
pip uninstall django.
/Lib/site-packages, deleted django folder.
Deleted all *.pyc files in my project.
Deleted all __pycache__ folders in my project.
Cleared all migrations folders in my project (keep __init__.py).
pip install django==x.x.x.
python manage.py migrate.
python manage.py makemigrations <app_name>.
python manage.py migrate.
python manage.py runserver.
其中,倒数第二步的 python manage.py migrate可能会出现:
django.db.utils.InternalError: (1050, "Table 'XXX' already exists")
这样的错误,执行如下即可:
python manage.py migrate --fake
倒数第三步的app_name要保证是准确的,否则会出现:
App XXX could not be found. Is it in INSTALLED_APPS?
这样的错误,去settings.py找INSTALLED_APPS,保证app_name在其中即可。
(3)(urls.W005) URL namespace 'admin' isn't unique. You may not be able to reverse all URLs in this namespace
打开urls.py,注释掉其中的:
#url(r'admin/', admin.site.urls),
终于可以跑起来了!