服务器错误(500)在Heroku上进行Django部署

时间:2018-07-22 14:40:39

标签: python django heroku django-settings server-error

我在settings.py文件中将debug设置为False(请注意,在将其设置为false之前,一切都正常进行了工作),当我运行create table "Trial Balance 1617" ("Document Date" date, "GL Account Code" varchar(25), "Debit Amount" numeric(12,2), "Credit Amount" numeric(12,2)) create table "GL Master Account" ("GL Account Code" varchar(25), "Opening Balance" numeric(12,2)) insert into "GL Master Account" values ('A3010101B058', '8110339.14') insert into "Trial Balance 1617" values ('4/1/2016', 'A3010101B058', 5332269.28, 0) insert into "Trial Balance 1617" values ('4/2/2016', 'A3010101B058', 741674.9, 0) insert into "Trial Balance 1617" values ('4/4/2016', 'A3010101B058', 570253.96, 0) insert into "Trial Balance 1617" values ('4/5/2016', 'A3010101B058', 0, -148839.52) SELECT t1."Document Date" as txn_dt, t1."GL Account Code" as gl_code, t1."Debit Amount" as debit, t1."Credit Amount" as credit, case when not exists (select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date") then t2."Opening Balance" else t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date") - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") end as op_bal, case when not exists (select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date") then t2."Opening Balance" else t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date") - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") end + t1."Debit Amount" + t1."Credit Amount" as closing_bal FROM "Trial Balance 1617" t1 INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code] WHERE t1."GL Account Code" = 'A3010101B058' 并转到我的网站时,主页正在运行,但是并非我的所有页面都这样:在某些页面上,我收到一个错误消息,说txn_dt gl_code debit credit op_bal closing_bal 2016-04-01 A3010101B058 5332269.28 0.00 8110339.14 13442608.42 2016-04-02 A3010101B058 741674.90 0.00 13442608.42 14184283.32 2016-04-04 A3010101B058 570253.96 0.00 14184283.32 14754537.28 2016-04-05 A3010101B058 0.00 -148839.52 14754537.28 14605697.76

这是我的;with cte_op_balance as ( SELECT t1."Document Date" as txn_dt, t1."GL Account Code" as gl_code, case when not exists (select * from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date") then t2."Opening Balance" else t2."Opening Balance" + (select sum("Debit Amount") from "Trial Balance 1617" where "GL Account Code" = t1."GL Account Code" and "Document Date" < t1."Document Date") - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") - (select sum("Credit Amount") from "Trial Balance 1617" where "GL Account Code" = t2."GL Account Code" and "Document Date" < t1."Document Date") end as op_bal FROM "Trial Balance 1617" t1 INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code] WHERE t1."GL Account Code" = 'A3010101B058' ) SELECT t1."Document Date" as txn_dt, t1."GL Account Code" as gl_code, t1."Debit Amount" as debit, t1."Credit Amount" as credit, c.op_bal, c.op_bal + t1."Debit Amount" + t1."Credit Amount" as closing_bal FROM "Trial Balance 1617" t1 INNER JOIN "GL Master Account" t2 ON t1.[GL Account Code] = t2.[GL Account Code] INNER JOIN cte_op_balance c ON t1.[GL Account Code] = c.gl_code and t1.[Document Date] = c.txn_dt WHERE t1."GL Account Code" = 'A3010101B058' 代码:

git push heroku master

我注意到的是我传递ID的网址无效

如果您知道答案,请帮助我

3 个答案:

答案 0 :(得分:0)

我有一个类似的问题,很难弄清楚DEBUG何时设置为False。当我将其重新配置回DEBUG = True时,一切恢复正常。所以问题是

  

您通常不会在开发中看到此错误,因为在DEBUG = True时,ManifestStaticFilesStorage切换为非哈希网址。   https://stackoverflow.com/a/51060143/7986808

您所遇到问题的解决方案可能与我的不同。但是您可以通过更改Django项目的日志记录方法来发现问题,因此即使在heroku logs -t -a <heroku-app>模式下运行DEBUG = False时,也可以在命令行中查看更多信息。

按以下步骤更改settings.py中的Django日志记录设置:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': ('%(asctime)s [%(process)d] [%(levelname)s] '
                       'pathname=%(pathname)s lineno=%(lineno)s '
                       'funcname=%(funcName)s %(message)s'),
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        }
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}

然后运行heroku logs -t -a <heroku-app>并打开您之前遇到500错误的URL,您将在日志中找到问题所在。希望对您有帮助。


万一有些静态文件丢失,只需按照settings.py切换STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')中的静态根目录即可。运行heroku run python manage.py collectstatic -a <heroku-app>将创建staticfiles目录。

答案 1 :(得分:0)

我有同样的错误。

我通过删除import django heroku中的settings.py来解决了问题

答案 2 :(得分:0)

我在 heroku 上部署 react 和 django 项目时遇到了同样的错误。 我已经调试了 2 天,做了所有事情,比如允许的主机、静态文件、json、whitenoise 的事情,但仍然没有解决问题。

如果您的浏览器抛出错误 500,则在 DEBUG=True 中设置 settings.py。如果它在 index.html 中引发 TemplateDoesNotExist 错误,则更改 views.py 可能有助于解决该问题。部署前不要忘记设置DEBUG=False

我通过更改 views.py 中的一些代码解决了这个问题。

def index(request):

return render(request,'ur_app_name.html')  

def index(request):

some_variable_name=TemplateResponse(request,'ur_app_name.html',{})

return some_variable_name

不要忘记在views.py之上导入这个

from django.template.response import TemplateResponse

我不知道我面临的具体问题是什么,我猜这是一个 httpResponse 问题,因为服务器需要 http 请求而我们的本地机器不需要它。

此错误的一些关键点:

  1. 确保您在 ALLOWED_HOST 文件的 settings.py 中提及您的域名
  2. set STATIC_ROOT=os.path.join(BASE_DIR, 'staticfiles') in settings.py,当 heroku 抛出一个 collecticstatic 错误,但我不确定。如果它仍然抛出 同样的错误然后在命令上禁用 collectstatic=1(你可以找到 调试日志中的确切命令)不会创建 任何问题。
  3. whitenoise.middleware.WhiteNoiseMiddlewareMIDDLEWARE 中设置 settings.py,这可能会有所帮助。
  4. 如果您的项目使用 react 和 django,则仅在 heroku 上添加构建包时选择 python。不要选择 node.js。

这是我上传 Heroku 项目的 github repository,您可以在此处找到可能有用的配置文件。

如果您的项目使用 react 和 django 以及以上建议没有帮助,这可能会有所帮助:

  • 检查 .gitignore 个文件,安装所有 requirement.txt 个文件 并复制根目录下的 package.json 文件。

设置 "build": "cd <project folder name>&& react-scripts build",在脚本中 package.json

我的 index.html 和 manifest.json 在上面提到的 github repo 中。 'staticfiles' 目录是使用 '$ python manage.py collectstatic' 在根文件夹中创建的。如果您的项目没有,请尝试复制我的 index.html 文件。 仅此而已。

herokuapp 链接:https://sainterface.herokuapp.com/