2014-11-23 76 views

回答

1

該視圖的dispatch方法是返回HttpResponse的方法。你總是可以提出一個不會在別處處理的異常並在那裏捕獲它。它應該泡起來,直到它到達那裏。

Ex。

#place where you want to raise exception 
if condition.is_not_met: 
    raise WoahException 

#views.py 
class MyFormView(views.FormView): 
    def dispatch(self, request, *args, **kwargs): 
     try: 
      return super(MyFormView, self).dispatch(request, *args, **kwargs) 
     except WoahException: 
      return HttpResponse("Woah, there was an exception") 
+0

完美,謝謝! – arctelix 2014-11-23 08:31:42

+0

我有另一個hickup,事實證明我實際上需要趕上在模板標籤中引發的異常。見http://stackoverflow.com/questions/27210643/django-how-to-catch-a-specific-exception-in-a-class-based-view-during-template – arctelix 2014-11-30 07:19:13

相關問題