2013-04-03 132 views
2

django的一個很棒的功能是在On 500錯誤上返回的調試頁面。Django調試錯誤頁面無法在AJAX調用上工作

但是,在AJAX調用中,我收到了在DEBUG = False時會出現的純文本錯誤。執行非AJAX請求時不會發生此問題:如果發生Internet服務器錯誤,則返回「正常」請求,然後返回漂亮的調試頁。

我昨天升級了從django 1.3升級到1.5。

我的settings.py有folllowing:

DEBUG = True 
TEMPLATE_DEBUG = True 
TEMPLATE_CONTEXT_PROCESSORS contain django.core.context_processors.debug 

我已經重置了settings.py爲 '出廠設置',但問題仍然存在。受影響的最簡單的代碼

例子 - 看到,即使是最簡單的代碼受到影響:

def webservice(request): 
    raise KeyError 
    return HttpResponse('it didnt work') 

這是由下面的請求被稱爲:

$.post('/webservice/', {'a':1, 'b':2}) 

必須指出的是,我使用jquery,並且這個小寶貝如此將csrf_token添加到請求中:

$(document).ajaxSend(function(event, xhr, settings) { 
     function sameOrigin(url) { 
      var host = document.location.host, // host + port 
       protocol = document.location.protocol, 
       sr_origin = '//' + host, 
       origin = protocol + sr_origin; 
      return (url == origin || url.slice(0, origin.length + 1) == origin + '/') || 
       (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') || 
       // or any other URL that isn't scheme relative or absolute i.e relative. 
       !(/^(\/\/|http:|https:).*/.test(url)); 
     } 
     function safeMethod(method) { 
      return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); 
     } 
     if (!safeMethod(settings.type) && sameOrigin(settings.url)) { 
      xhr.setRequestHeader("X-CSRFToken", '{{csrf_token}}'); 
     } 
    }); 

解決方案

行爲在1.4中更改爲僅當請求是ajax時返回回溯。下面是如何有選擇地把功能回(這是非常有用的,當你的工作主要是在Ajax請求的域名):從

if request.is_ajax(): 

編輯django.views.debug 65行

if request.is_ajax() and settings.MINIFY_505_ON_AJAX

然後在settings.py

+0

請包含您的某個AJAX請求的代碼。 – 2013-04-03 19:55:12

回答

2

在「好調試頁」取決於調試環境(從https://docs.djangoproject.com/en/dev/ref/settings/#debug

One of the main features of debug mode is the display of detailed error pages. 
If your app raises an exception when DEBUG is True, Django will display a 
detailed traceback, including a lot of metadata about your environment, such 
as all the currently defined Django settings (from settings.py). 

TEMPLATE_DEBUG增加了有關模板渲染錯誤的額外信息。

UPDATE

這是它應該工作,如果request.is_ajax()爲true,那麼文本掃描是在響應主體返回的路上,檢查源:

https://github.com/django/django/blob/master/django/views/debug.py#L59-70

此行爲已在1.4中更改,提交:https://github.com/django/django/commit/0d9b6a5bc43c06716212bd3f847460ce985381aa

UPDATE 2

這有點不好意思,但出於調試目的,您可以修改HTTP_X_REQUESTED_WITH標頭,以便request.is_ajax()爲false,然後強制執行html響應。 (請參閱https://github.com/django/django/blob/master/django/http/request.py#L136-137

+0

我的settings.py包含以下內容: DEBUG = True 此外,問題只發生在AJAX請求上。 – rikAtee 2013-04-03 20:42:32

+0

@rikAtee:這不是問題,這是它的意思,檢查我的更新與源的鏈接。 – gonz 2013-04-03 21:06:03

+0

你真棒!謝謝。我做了以下內容: 編輯'technical_500_response'爲'如果request.is_ajax()和settings.MINIFY_505_ON_AJAX:'和設置添加MINIFY_505_ON_AJAX =假 這是一個更容易維護修復,什麼是Django的意思是,如果不維護! – rikAtee 2013-04-03 21:25:38

1

的添加選項僅適用於呈現Django模板時發生的錯誤。由於你的函數不提供任何模板,所以該選項將不起作用。

https://docs.djangoproject.com/en/dev/ref/settings/#template-debug

接通/關模板調試模式的布爾值。如果這是真的,則 花哨錯誤頁面將顯示關於在模板渲染期間產生的任何異常 的詳細報告。此報告包含模板的相關 片段,並突出顯示相應的行。

這也意味着即使您的函數確實呈現模板,如果錯誤發生在模板呈現之前,您仍然會以純文本輸出結束。

注意,在下面的例子:

def year_archive(request, year): 
    a_list = Article.objects.filter(pub_date__year=year) 
    raise Exception("something horrible happened") 
    return render_to_response('news/year_archive.html', {'year': year, 'article_list': a_list}) 

Exceptionrender_to_response之前提出的,所以TEMPLATE_DEBUG將永遠不會有機會運行。

如果您想查看非模板錯誤的回溯,則需要查看命令行輸出或日誌。

+0

嗯 - 奇怪。在Django 1.3中,我正在獲得完整的調試頁面。 – rikAtee 2013-04-03 20:09:47

+0

從1.3開始,功能似乎已經改變。以下是該文檔版本的說明:「一個打開/關閉模板調試模式的布爾值,如果這是True,那麼花哨的錯誤頁面將顯示任何TemplateSyntaxError的詳細報告,該報告包含模板的相關片段,並突出顯示適當的行。「 – 2013-04-03 20:13:59

+0

我使用提供的示例代碼,仍然可以看到調試頁面: http://i.imgur.com/QCOBCdr.png – rikAtee 2013-04-03 20:15:40