gpt4 book ai didi

python - Heroku+Django+Postgres错误: "FATAL: database "python_getting_started"does not exist"

转载 作者:行者123 更新时间:2023-11-29 13:56:28 27 4
gpt4 key购买 nike

我正尝试在 Heroku ( https://devcenter.heroku.com/articles/getting-started-with-python#introduction ) 上做 Python 入门练习。我只是按照说明使用教程中提供的 git 存储库。但是当我转到使用数据库功能的 Web 应用程序的/db 页面时,我得到了这个错误跟踪:


Environment:


Request Method: GET
Request URL: http://localhost:5000/db

Django Version: 1.8.1
Python Version: 2.7.5
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware')


Traceback:
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/hello/views.py" in db
14. greeting.save()
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/models/base.py" in save
710. force_update=force_update, update_fields=update_fields)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/models/base.py" in save_base
735. with transaction.atomic(using=using, savepoint=False):
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/transaction.py" in __enter__
150. if not connection.get_autocommit():
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in get_autocommit
286. self.ensure_connection()
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in ensure_connection
130. self.connect()
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/utils.py" in __exit__
97. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in ensure_connection
130. self.connect()
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/base/base.py" in connect
119. self.connection = self.get_new_connection(conn_params)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py" in get_new_connection
172. connection = Database.connect(**conn_params)
File "/Users/Lisha/Desktop/Starred_Docs/python-getting-started/venv/lib/python2.7/site-packages/psycopg2/__init__.py" in connect
164. conn = _connect(dsn, connection_factory=connection_factory, async=async)

Exception Type: OperationalError at /db
Exception Value: FATAL: database "python_getting_started" does not exist

只有当我在本地运行应用程序时才会崩溃; herokuapp.com 上的应用程序运行良好。你知道问题可能是什么吗?非常感谢您的帮助。

最佳答案

#try to load local_settings.py if it exists
try:
from .settings_local import *
except Exception as e:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
}
}
import dj_database_url
DATABASES['default'] = dj_database_url.config()
# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
# Allow all host headers
ALLOWED_HOSTS = ['']

在您的 settings.py 附近创建 settings_local.py 并在此处声明您的数据库设置

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'python_getting_started',
'USER': 'some_postgre_user',
'PASSWORD': 'some_password',
}
}

如果您没有 python_getting_started 显然您必须创建它:

psql postgres
create database python_getting_started;
create user some_postgre_user password 'some_password';
grant all on database python_getting_started to some_postgre_user;
\q

关于python - Heroku+Django+Postgres错误: "FATAL: database "python_getting_started"does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30816181/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com