Django 设置 DigitalOcean Postgres "DATABASES = {" 错误
Django Setting up DigitalOchean Postgre "DATABASES = {" ERORR
我正在按照 https://www.udemy.com/course/the-ultimate-beginners-guide-to-django-django-2-python-web-dev-website/learn/lecture/9517168#overview 指南在带有 Postgres 数据库的 Digital Ocean Server 上构建 Django 网站。
老师遵循的指南是官方的Digital Ocean设置指南https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#creating-and-configuring-a-new-django-project
一切都很好,直到我尝试在服务器端迁移项目:
我在终端运行中使用的第一种语法
python manage.py makemigrations
第一个错误
File "manage.py", line 16
) from exc
^
SyntaxError: invalid syntax
我在终端运行中使用的第二种语法
python3 manage.py makemigrations
第二个错误
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 317, in execute
settings.INSTALLED_APPS
File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup
self._wrapped = Settings(settings_module)
File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/djangodeploy/portfolio-project/portfolio/settings.py", line 137, in <module>
from .local_settings import *
File "/home/djangodeploy/portfolio-project/portfolio/local_settings.py", line 5
DATABASES = {
^
IndentationError: unexpected indent
我修改了原来的settings.py文件,把下面的内容放在最下面,如
try:
from .local_settings import *
except ImportError:
pass
- 我也尝试了导入设置更正(更改了用于发布的数据库数据的值)Django Local Settings
from settings import *
SECRET_KEY = 'MY_SECRET_KEY'
DEBUG = False
ALLOWED_HOSTS = ['164.143.264.187']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'my_name',
'USER': 'the_user_nameing_i_have',
'PASSWORD': 'my_key',
'HOST':'localhost',
'POSRT': '6432',
}
}
我仍然得到原来的错误。
我的 manage.py 文件我已经尝试过这两个选项
"""
# this commented out what the teacher has in his code
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "portfolio.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
"""
# This is what I had
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'portfolio.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
根据 Pranita Gandhi 的评论更新 1
我卸载了服务器 VENV 中的所有 4 个库
在本地机器上我创建了一个 requirements.txt 的原始 venv 作为
Pranita Gandhi 推荐它。比把它放到服务器上并尝试将它安装到现在空的服务器 venv 上。
pip3 install -r requirements.txt
- 我得到以下错误:
Collecting Django==2.0.7
Using cached https://files.pythonhosted.org/packages/ab/15/cfde97943f0db45e4f999c60b696fbb4df59e82bbccc686770f4e44c9094/Django-2.0.7-py3-none-any.whl
Collecting Pillow==5.0.0
Using cached https://files.pythonhosted.org/packages/9a/2f/86941111d108fd060190c994f15881283b98693c1c370e74885cfc470eb3/Pillow-5.0.0-cp36-cp36m-manylinux1_x86_64.whl
Collecting psycopg2==2.7.4
Using cached https://files.pythonhosted.org/packages/92/15/92b5c363243376ce9cb879bbec561bba196694eb663a6937b4cb967e230e/psycopg2-2.7.4-cp36-cp36m-manylinux1_x86_64.whl
Collecting psycopg2-binary==2.7.4
Using cached https://files.pythonhosted.org/packages/5f/0b/aa7078d3f6d27d951c38b6a1f4b99b71b2caecebb2921b2d808b5bf0e2e0/psycopg2_binary-2.7.4-cp36-cp36m-manylinux1_x86_64.whl
Collecting pytz==2018.3
Using cached https://files.pythonhosted.org/packages/3c/80/32e98784a8647880dedf1f6bf8e2c91b195fe18fdecc6767dcf5104598d6/pytz-2018.3-py2.py3-none-any.whl
Installing collected packages: pytz, Django, Pillow, psycopg2, psycopg2-binary
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/lib/python3.6/site-packages'
Consider using the `--user` option or check the permissions.
更新 2
我试图强制安装基于
https://github.com/googlesamples/assistant-sdk-python/issues/236 / moham96 于 2018 年 4 月 20 日发表评论
您正在尝试将程序包安装到您没有写入权限的系统文件夹中。
您有三个选项(只使用其中一个):
- 1-设置一个虚拟环境来安装包(推荐):
python3 -m venv env
source ./env/bin/activate
python -m pip install requirements.txt
- 2-安装包到用户文件夹:
python -m pip install --user requirements.txt
- 3-使用sudo安装到系统文件夹(不推荐)
sudo python -m pip install requirements.txt
不幸的是,这些都没有解决我的问题。
您好,您是否在课程中出现提示时安装并激活了虚拟环境。我认为它可能是虚拟环境。
你能在服务器端找到你的 venv 或 env 文件夹吗?
我正在按照 https://www.udemy.com/course/the-ultimate-beginners-guide-to-django-django-2-python-web-dev-website/learn/lecture/9517168#overview 指南在带有 Postgres 数据库的 Digital Ocean Server 上构建 Django 网站。
老师遵循的指南是官方的Digital Ocean设置指南https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-18-04#creating-and-configuring-a-new-django-project
一切都很好,直到我尝试在服务器端迁移项目:
我在终端运行中使用的第一种语法
python manage.py makemigrations
第一个错误
File "manage.py", line 16 ) from exc ^ SyntaxError: invalid syntax
我在终端运行中使用的第二种语法
python3 manage.py makemigrations
第二个错误
Traceback (most recent call last): File "manage.py", line 21, in <module> main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line utility.execute() File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 317, in execute settings.INSTALLED_APPS File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__ self._setup(name) File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 43, in _setup self._wrapped = Settings(settings_module) File "/home/djangodeploy/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__ mod = importlib.import_module(self.SETTINGS_MODULE) File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 994, in _gcd_import File "<frozen importlib._bootstrap>", line 971, in _find_and_load File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 665, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 678, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/home/djangodeploy/portfolio-project/portfolio/settings.py", line 137, in <module> from .local_settings import * File "/home/djangodeploy/portfolio-project/portfolio/local_settings.py", line 5 DATABASES = { ^ IndentationError: unexpected indent
我修改了原来的settings.py文件,把下面的内容放在最下面,如
try:
from .local_settings import *
except ImportError:
pass
- 我也尝试了导入设置更正(更改了用于发布的数据库数据的值)Django Local Settings
from settings import *
SECRET_KEY = 'MY_SECRET_KEY'
DEBUG = False
ALLOWED_HOSTS = ['164.143.264.187']
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'my_name',
'USER': 'the_user_nameing_i_have',
'PASSWORD': 'my_key',
'HOST':'localhost',
'POSRT': '6432',
}
}
我仍然得到原来的错误。
我的 manage.py 文件我已经尝试过这两个选项
"""
# this commented out what the teacher has in his code
import os
import sys
if __name__ == "__main__":
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "portfolio.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
"""
# This is what I had
import os
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'portfolio.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
根据 Pranita Gandhi 的评论更新 1
我卸载了服务器 VENV 中的所有 4 个库
在本地机器上我创建了一个 requirements.txt 的原始 venv 作为 Pranita Gandhi 推荐它。比把它放到服务器上并尝试将它安装到现在空的服务器 venv 上。
pip3 install -r requirements.txt
- 我得到以下错误:
Collecting Django==2.0.7
Using cached https://files.pythonhosted.org/packages/ab/15/cfde97943f0db45e4f999c60b696fbb4df59e82bbccc686770f4e44c9094/Django-2.0.7-py3-none-any.whl
Collecting Pillow==5.0.0
Using cached https://files.pythonhosted.org/packages/9a/2f/86941111d108fd060190c994f15881283b98693c1c370e74885cfc470eb3/Pillow-5.0.0-cp36-cp36m-manylinux1_x86_64.whl
Collecting psycopg2==2.7.4
Using cached https://files.pythonhosted.org/packages/92/15/92b5c363243376ce9cb879bbec561bba196694eb663a6937b4cb967e230e/psycopg2-2.7.4-cp36-cp36m-manylinux1_x86_64.whl
Collecting psycopg2-binary==2.7.4
Using cached https://files.pythonhosted.org/packages/5f/0b/aa7078d3f6d27d951c38b6a1f4b99b71b2caecebb2921b2d808b5bf0e2e0/psycopg2_binary-2.7.4-cp36-cp36m-manylinux1_x86_64.whl
Collecting pytz==2018.3
Using cached https://files.pythonhosted.org/packages/3c/80/32e98784a8647880dedf1f6bf8e2c91b195fe18fdecc6767dcf5104598d6/pytz-2018.3-py2.py3-none-any.whl
Installing collected packages: pytz, Django, Pillow, psycopg2, psycopg2-binary
ERROR: Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/lib/python3.6/site-packages'
Consider using the `--user` option or check the permissions.
更新 2
我试图强制安装基于 https://github.com/googlesamples/assistant-sdk-python/issues/236 / moham96 于 2018 年 4 月 20 日发表评论
您正在尝试将程序包安装到您没有写入权限的系统文件夹中。 您有三个选项(只使用其中一个):
- 1-设置一个虚拟环境来安装包(推荐):
python3 -m venv env
source ./env/bin/activate
python -m pip install requirements.txt
- 2-安装包到用户文件夹:
python -m pip install --user requirements.txt
- 3-使用sudo安装到系统文件夹(不推荐)
sudo python -m pip install requirements.txt
不幸的是,这些都没有解决我的问题。
您好,您是否在课程中出现提示时安装并激活了虚拟环境。我认为它可能是虚拟环境。
你能在服务器端找到你的 venv 或 env 文件夹吗?