Django 框架在 DEBUG 为 False 时出现服务器错误 500

时间:2021-02-06 17:51:38

标签: django server render server-side-rendering http-status-code-500

我在项目中使用 Django 框架。我正在渲染 HTML 模板

def index(request):
    print(" --- check fine?")            # for print debug
    trees = Tree.objects.all()
    print(trees)
    return render(request, "index.html", {'trees': trees})

 --- check fine?
<QuerySet [<Tree: Tree object (8)>, <Tree: Tree object (10)>, <Tree: Tree object (11)>, <Tree: Tree object (12)>]>
[06/Feb/2021 17:28:44] "GET / HTTP/1.1" 500 145

这些是我的网址。


urlpatterns = [
     path("", views.index, name="index"),
     path('about/', views.about , name='about'),
     path('contact/', views.contact , name='contact'),
     path('upload/', views.upload_file , name='upload'),
    ]

DEBUG = False 我遇到了上述错误。

当我设置我的 DEBUG = True 一切正常意味着显示 index.html 并显示偶数数据。

设置 DEBUG = False 使查找错误变得困难。

index.html 包含以下获取数据的代码。

{% for tree in trees %}
    <article class="col-lg-3 col-md-4 col-sm-6 col-12 tm-gallery-item">
        <figure>
            <img src="{{tree.image.url}}" alt="Image" class="img-fluid tm-gallery-img" />
                <figcaption>
                    <h4 class="tm-gallery-title">{{tree.name}}</h4>
                    <p class="tm-gallery-description">{{tree.desc}}</p>
                    <!-- <p class="tm-gallery-price"></p> -->
                </figcaption>
        </figure>
    </article>
{% endfor %}

允许的主机是。

ALLOWED_HOSTS = ['*','127.0.0.1','localhost']

2 个答案:

答案 0 :(得分:0)

看看这个:

  1. 当 Django 在视图中遇到运行时错误(例如语法错误或视图未能返回 Django 期望的对象)时,Django 会返回 HTTP 状态代码 500,即内部服务器错误。与视图逻辑中的错误密切相关的是,当视图中遇到错误、DEBUG 设置为 False、未找到 500.html 模板时,Django 本身会引发 TemplateDoesNotExist 异常。

  2. 从 Django 1.5 开始,引入了一个新的“允许的主机”设置。当 DEBUG 设置为 False 时,它​​会被强制执行。在您的 settings.py 文件中,找到如下所示的:

ALLOWED_HOSTS = [] 并将其更改为包含 Droplet 的 IP 地址和/或指向您站点的域名。例如:

ALLOWED_HOSTS = ["example.com", "111.111.111.111"]

来源:

  1. https://docs.webfaction.com/software/django/troubleshooting.html#:~:text=Django%20returns%20an%20HTTP%20status,an%20object%20that%20Django%20expects
  2. https://www.digitalocean.com/community/questions/server-error-500-django-digital-ocean

答案 1 :(得分:0)

这是 settings.py 中的变量

STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
    
]
MEDIA_ROOT  = os.path.join(BASE_DIR, 'media')
MEDIA_URL = '/media/'
# STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"

由于 STATICFILES_STORAGE collectstatic 使用了白噪声引擎。我评论 STATICFILES_STORAGE 。 我建议删除上述案例 /staticfiles 文件夹中的所有 STATIC_ROOT 目录。

并使用 python manage.py collectstatic 收集文件。

并在网址中使用它

from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('', include('ourhouse.urls')),
    path('admin/', admin.site.urls),
    # path('accounts/',include('accounts.urls'))
]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

使用静态和媒体文件。

相关问题