【发布时间】:2015-04-25 13:58:12
【问题描述】:
部署到heroku(在我的网络浏览器中)后我收到错误消息:
OperationalError at /accounts/register/
could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
在settings.py 我有:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'mydata',
'USER': [a name],
'PASSWORD': [a password],
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
我在 heroku 日志中看到一些抱怨:
2015-04-25T07:53:35.555555+00:00 heroku[router]: sock=client at=error code=H18 desc="Request Interrupted" method=GET path="/accounts/register/" host=myapp.herokuapp.com request_id=[long string] fwd="43.55.555.555" dyno=web.1 connect=1ms service=119ms status=503 bytes=13000
2015-04-25T07:56:49.555555+00:00 heroku[web.1]: State changed from up to starting
2015-04-25T07:56:52.555555+00:00 heroku[web.1]: Stopping all processes with SIGTERM
但在那之后它会做更多的事情。最后一系列消息的结尾是:
2015-04-25T13:30:06:555555+00:00 heroku[web.1]: State changed from starting to up
和 6 个摘要,状态为 301、404、404、292、404、404
我的wsgi.py 没有改变:
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dating_site.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
我什至在我的 proc 中添加了完整路径:
web: gunicorn mysite.wsgi --pythonpath ../mysite --log-file -
../mysite 在这里我说的是主要的 mysite 目录,其中包含 settings.py、wsgi.py 和其他一些好东西。
当我访问myapponheroku/accounts 的网页时,我得到了一个漂亮的索引页面,但是当我点击“注册”时,我在条目顶部看到了错误......真正让我感到困惑的是什么? accounts/login 呈现一个需要 csrf_token 的表单......它加载得很好! accounts/register 呈现一个表单,当然需要 csrf ......它抱怨 could not connect to server: Connection refused。有趣的是,如果您从页面顶部将accounts/register 替换为accounts/login,则尝试使用我的测试用户名和密码实际登录登录页面会产生相同的错误('操作错误' yada yada)
什么会导致/解决这个奇怪的问题?谢谢 django 明星们,
科迪
【问题讨论】:
-
我认为问题出在 settings.py 中的 ALLOWED_HOSTS = [ ] ....我的应用名称已更改为“cody-my-app.herokuapp.com”...我要添加 'cody -my-app' 或整个字符串到 ALLOWED_HOSTS,每次我进行更改以查看它们在 heroku 上的工作时,我是否必须 git push heroku master?这是我第一次认真使用heroku
-
现在问题更严重了。按照有关 heroku 的其他说明,我添加到 wsgi.py:
import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "dating_site.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() from django.core.wsgi import get_wsgi_application from dj_static import Cling application = Cling(get_wsgi_application()) -
在 settings.py 我添加:在 settings.py 我添加:
import dj_database_url DATABASES['default'] = dj_database_url.config() SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') ALLOWED_HOSTS = ['*'] BASE_DIR = os.path.dirname(os.path.dirpath(__file__)) STATIC_ROOT = 'staticfiles' STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) -
应该是“BASE_DIR = os.path.dirname(os.path.abspath(file))”吗?
-
我应该提到这些更改,应用程序甚至不会再加载,尽管没有 Heroku.com 建议的设置,我至少可以正常工作:
Application Error An error occurred in the application and your page could not be served. Please try again in a few moments.If you are the application owner, check your logs for details.
标签: python django heroku web-deployment operationalerror