答えて

14

エラーメールに、ユーザーに関する情報をアタッシェするためにあなたはこの単純なミドルウェアを使用することができます歩哨を使用しない場合:

# source: https://gist.github.com/646372 
class ExceptionUserInfoMiddleware(object): 
    """ 
    Adds user details to request context on receiving an exception, so that they show up in the error emails. 

    Add to settings.MIDDLEWARE_CLASSES and keep it outermost(i.e. on top if possible). This allows 
    it to catch exceptions in other middlewares as well. 
    """ 

    def process_exception(self, request, exception): 
     """ 
     Process the exception. 

     :Parameters: 
      - `request`: request that caused the exception 
      - `exception`: actual exception being raised 
     """ 

     try: 
      if request.user.is_authenticated(): 
       request.META['USERNAME'] = str(request.user.username) 
       request.META['USER_EMAIL'] = str(request.user.email) 
     except: 
      pass 

あなたは、単にどこでも*の.pyファイルにこのクラスを置くことができますあなたのDjangoプロジェクトの下にあり、MIDDLEWARE_CLASSESへの参照を追加してください。私。プロジェクトルート(あなたのsettings.pyがある)のファイル "ミドルウェア"に入れた場合、単にmiddleware.ExceptionUserInfoMiddlewareを追加するだけです。

+0

これは簡単です。だからこそ、Djangoが電子メールに含めるものにこれらの2つの値を追加するだけです。その後、Djangoは電子メールを正常に送信しますか? – Greg

+0

このクラスをどのファイルに入れますか?そして、私はそれをインポートし、settings.pyから参照するだけですか? – Greg

+0

@Greg私は答えを更新しました。 –

関連する問題