服务器错误(500)在Heroku上进行Django部署 [英] Server error (500) Django deployment on heroku

查看:188
本文介绍了服务器错误(500)在Heroku上进行Django部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在settings.py文件中将调试设置为False(请注意,在将其设置为false之前,一切都正常进行了工作),当我运行git push heroku master并转到我的网站时,主页正在运行,但当时我所有的页面都不是这样:在某些页面上,我收到一条错误消息,提示Server Error (500)

I turned debug to False in my settings.py file (note that before I turned it to false, everything was workign perfectly) and when I ran git push heroku master and went to my website, the home page was working but that wasn't the case for all of my pages: on some, I got an error saying Server Error (500)

这是我的settings.py代码:

DEBUG = False
ALLOWED_HOSTS = ["hacka-labs.herokuapp.com"]

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

What I have noticed is that the urls where I pass an id aren't working

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

Please help me if you know the answer

推荐答案

我遇到了类似的问题,很难弄清DEBUG何时设置为False.当我将其重新配置回DEBUG = True时,一切再次正常运行.所以问题是

I had a similar problem and it's hard to figure out when DEBUG is set to False. When I reconfigured it back to DEBUG = True everything worked fine again. So the problem was

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

You don't generally see this error in development because when DEBUG = True, ManifestStaticFilesStorage switches to non-hashed urls. https://stackoverflow.com/a/51060143/7986808

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

The solution to the problem in your case may differ from mine. But you can find the problem by changing the logging method of your Django project, so you can see more info in the command line when running heroku logs -t -a <heroku-app> even in DEBUG = False mode.

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

Change your django logging settings in settings.py as follow:

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错误的网址,您将在日志中找到问题所在.希望对您有帮助.

Then run heroku logs -t -a <heroku-app> and open the url where you previously got 500 Error you will find out the problem in logs. Hope this will help you.

如果缺少某些静态文件,只需按照STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')的说明在settings.py中切换您的静态根目录.运行heroku run python manage.py collectstatic -a <heroku-app>会创建staticfiles目录.

In case there are some static files missing just switch your static root in settings.py as follow STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles'). Running heroku run python manage.py collectstatic -a <heroku-app> creates staticfiles directory.

这篇关于服务器错误(500)在Heroku上进行Django部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆