sim*_*mao 12 python django http
在开发过程中,我在调试模式下运行Django,我使用文本模式应用程序将数据发布到我的应用程序.理想情况下,当我收到http错误代码500时,我需要收到纯文本响应,因此我不必在HTML和Javascript中查找真正的错误.
是否可以获得Django 500内部服务器错误作为纯文本?
Mar*_*ano 19
如果您正在寻找一种在使用时获取纯文本错误页面的方法curl
,则需要添加X-Requested-With
带有值的HTTP标头XMLHttpRequest
,例如
curl -H 'X-Requested-With: XMLHttpRequest' http://example.com/some/url/
Run Code Online (Sandbox Code Playgroud)
说明:这是因为Django使用该is_ajax
方法来确定是以纯文本还是以HTML格式返回.is_ajax
反过来看X-Requested-With
.
我想写一个中间件,因为否则500.html中没有例外
http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception
class ProcessExceptionMiddleware(object):
def process_exception(self, request, exception):
t = Template("500 Error: {{ exception }}")
response_html = t.render(Context({'exception' : exception }))
response = http.HttpResponse(response_html)
response.status_code = 500
return response
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2811 次 |
最近记录: |