简体   繁体   English

服务器错误 (500) 在 heroku 上部署 Django

[英]Server error (500) Django deployment on heroku

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(请注意,在我将其设置为 false 之前,一切都完美无缺)并且当我运行git push heroku master并访问我的网站时,主页正在运行,但那不是我所有页面的情况:在某些页面上,我收到一条错误消息,提示Server Error (500)

Here is my settings.py code :这是我的settings.py代码:

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

What I have noticed is that the urls where I pass an id aren't working我注意到的是,我传递 id 的网址不起作用

Please help me if you know the answer如果你知道答案,请帮助我

I had a similar problem and it's hard to figure out when DEBUG is set to False.我遇到了类似的问题,很难弄清楚 DEBUG 何时设置为 False。 When I reconfigured it back to DEBUG = True everything worked fine again.当我将它重新配置回DEBUG = True一切又正常了。 So the problem was所以问题是

You don't generally see this error in development because when DEBUG = True , ManifestStaticFilesStorage switches to non-hashed urls.您通常不会在开发中看到此错误,因为当DEBUG = True , ManifestStaticFilesStorage 会切换到非哈希网址。 https://stackoverflow.com/a/51060143/7986808 https://stackoverflow.com/a/51060143/7986808

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.但是您可以通过更改 Django 项目的日志记录方法来发现问题,因此即使在DEBUG = False模式下运行heroku logs -t -a <heroku-app>时,您也可以在命令行中看到更多信息。

Change your django logging settings in settings.py as follow: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,
        },
    }
}

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.然后运行heroku logs -t -a <heroku-app>并打开您之前收到 500 错误的网址,您将在日志中发现问题。 Hope this will help you.希望这会帮助你。


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') .如果缺少一些静态文件,只需按照STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')settings.py切换静态根。 Running heroku run python manage.py collectstatic -a <heroku-app> creates staticfiles directory.运行heroku run python manage.py collectstatic -a <heroku-app>创建 staticfiles 目录。

I had the same error.我有同样的错误。

I solved issue by removing import django heroku in settings.py我通过在settings.py删除import django heroku解决了问题

I had the same error while deploying react and django project on heroku.我在 heroku 上部署 react 和 django 项目时遇到了同样的错误。 I had been debugging for 2 days, doing all things like allowed host, staticfiles, json, whitenoise things but that still didn't solve the problem.我已经调试了 2 天,做了所有的事情,比如允许的主机、静态文件、json、whitenoise 的事情,但仍然没有解决问题。

If your browswer throws error 500 then set DEBUG=True in settings.py .如果您的浏览器抛出错误 500,则在settings.py设置DEBUG=True If it throws a TemplateDoesNotExist error at index.html then changing views.py may help solve that.如果它在 index.html 中引发 TemplateDoesNotExist 错误,则更改 views.py 可能有助于解决该问题。 Don't forget to set DEBUG=False before deploying.不要忘记在部署前设置DEBUG=False

I solved it by changing some code in views.py.我通过更改views.py中的一些代码解决了这个问题。

def index(request):

return render(request,'ur_app_name.html')  

TO

def index(request):

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

return some_variable_name

Dont't forgot to import this on top of views.py不要忘记在 views.py 之上导入这个

from django.template.response import TemplateResponse

I don't know what exact problem I was facing, I guess it's an httpResponse thing because server needs http request and our local machine does not need it.我不知道我面临的确切问题是什么,我想这是一个 httpResponse 问题,因为服务器需要 http 请求而我们的本地机器不需要它。

Some key points points for this error:此错误的一些关键点:

  1. Make sure you mention your domain name in ALLOWED_HOST in settings.py file确保在settings.py文件中的ALLOWED_HOST中提及您的域名
  2. set STATIC_ROOT=os.path.join(BASE_DIR, 'staticfiles') in settings.py , this maybe needed when heroku throws a collecticstatic error but I am not sure.STATIC_ROOT=os.path.join(BASE_DIR, 'staticfiles') in settings.py ,这也许当Heroku的抛出一个需要collecticstatic错误,但我不知道。 If it still throws the same error then disable collectstatic=1 on command (you can find the exact command for this in the debug log) which does not create any problem.如果它仍然抛出相同的错误,则在命令上禁用collectstatic=1 (您可以在调试日志中找到确切的命令),这不会产生任何问题。
  3. set whitenoise.middleware.WhiteNoiseMiddleware , in MIDDLEWARE in settings.py , this might help.settings.py中的MIDDLEWAREsettings.py whitenoise.middleware.WhiteNoiseMiddleware ,这可能会有所帮助。
  4. If your project uses react and django then choose python only on add build pack on heroku.如果您的项目使用 react 和 django,则仅在 heroku 上添加构建包时选择 python。 Do not choose node.js.不要选择 node.js。

This is the github repository where I upload my heroku project you can find the config files here which maybe helpful.这是我上传 Heroku 项目的github 存储库,您可以在此处找到可能有用的配置文件。

If your project uses react and django and above suggestions didn't help this may help:如果您的项目使用 react 和 django 及以上建议没有帮助,这可能会有所帮助:

  • check .gitignore files, install all requirement.txt files and copy package.json file in root directory.检查.gitignore文件,安装所有的requirement.txt文件并复制根目录下的package.json文件。

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

My index.html and manifest.json is in the github repo mentioned above.我的 index.html 和 manifest.json 在上面提到的 github repo 中。 The 'staticfiles' directory was created in the root folder using '$ python manage.py collectstatic '. “staticfiles”目录是使用“$ python manage.py collectstatic ”在根文件夹中创建的。 Try copying my index.html file if your project doesn't have one.如果您的项目没有,请尝试复制我的index.html文件。 That's all.就这样。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM