【问题标题】:Django Homepage 400 when debug=FalseDjango Homepage 400 时 debug=False
【发布时间】:2016-11-21 21:13:40
【问题描述】:

因此,当 Debug=True 时,我的应用程序在开发服务器中运行良好,但是,当我将其切换为 False 时,我的主页返回 400。我有一些返回 json 的端点,无论调试值如何,它们都能正常工作。

我正在使用 Django 1.10.2

urls.py

from django.conf.urls import url 
from django.contrib import admin
from fim_table import views


urlpatterns = [ 
    url(r'^$', views.create_home),
    url(r'^data/', views.data),
    ...
]

views.py

from django.shortcuts import render
from django.views.decorators.csrf import csrf_protect
from lockdown.decorators import lockdown
from .models import Fim, FimDeleted
from django.http import HttpResponse
from django.db.models.functions import Lower
from django.template.context_processors import csrf
import json


@csrf_protect
def create_home(request):
    return render(request, 'table.html', {'csrf': csrf})


# returns all of the data, unfiltered/response is json
@csrf_protect
def data(request):
    # show distinct names only
    fims = Fim.objects.annotate(name_lower=Lower('crib_name')).order_by('name_lower').distinct('name_lower')
    # fims need to be not a queryset but an array of dicts to be json
    dictionaries = [ idToString(name.as_dict()) for name in fims ]
    mydata = {"aaData": dictionaries}
    return HttpResponse(json.dumps(mydata), content_type='application/json')

settings.py

DEBUG = False
ALLOWED_HOSTS = ["*"]

更新 我实现了一些日志记录,并得到:

连接路径 (/DataTables/datatables.min.css) 位于基本路径组件 (/Users/me/development/my_project/myapp/staticfiles) 之外。

我认为这是一个白噪声问题,即使我设置我的 settings.py 与他们的 docs 中的完全一样

【问题讨论】:

  • 日志中没有任何内容?
  • [21/Nov/2016 21:46:36] "GET / HTTP/1.1" 500 27是吗
  • 这是错误 500,而不是错误 400。无论哪种方式,您都应该启用更多日志记录,以便获得实际的异常。
  • 尝试查看网络服务器错误日志,而不是访问日志。
  • 我更改了一些内容以获取 500 错误。现在日志正确显示了我的400 错误,我已经用一些日志信息更新了我的问题

标签: python django http-status-code-400


【解决方案1】:

尝试在允许的主机部分添加主机名。

For Ex: ALLOWED_HOSTS = ['localhost', 'localhost_projectname', 'server_hostname']

您可以在给定的博客文章中找到有关如何在您的网站中添加 404 和 500 页面的更多详细信息,

https://micropyramid.com/blog/handling-custom-error-pages-in-django/

【讨论】:

    猜你喜欢
    • 2017-01-20
    • 2013-11-21
    • 2021-10-30
    • 2013-12-03
    • 2016-03-11
    • 2018-01-07
    • 2013-03-25
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多