私はDjango(v1.11)プロジェクトに取り組んでおり、のミドルウェア機能をとしてサイトに提供する必要があります。 docsで説明したようにジャンゴのこのバージョンは古いMIDDLEWARE_CLASSES
設定を変更します。Django v1.11でミドルウェアクラスを正しく定義する方法は?
A new style of middleware was introduced for use with the new
MIDDLEWARE
setting.
しかし、私はミドルウェアがをどのように動作するかNEW理解することはできません。ミドルウェアのドキュメントを読んだ後、私は次のような結論に来る:
class FooMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
return self.get_response(request)
def process_template_response(self, request, response):
# do something here
return response
私はそれがうまくいくと思ったが、上記残念ながらコードが機能しないと、完全に任意のエラーなしジャンゴ(によって無視され! )。
私は次のように行うと:
class FooMiddleware(object):
def process_template_response(self, request, response):
# do something here
return response
を(object
クラスの__init__
メソッドは、引数を持っていないので)...エラーが発生しました:
TypeError: object() takes no parameters
コードを次のように変更すると、すべてが動作します:
from django.utils.deprecation import MiddlewareMixin
class FooMiddleware(MiddlewareMixin):
def process_response(self, request, response):
# do something here
return response
しかし、!MiddlewareMixin
は、互換性のためにdeprecation
utils関連して使用されます。
Django provides
django.utils.deprecation.MiddlewareMixin
to ease creating middleware classes that are compatible with bothMIDDLEWARE
and the oldMIDDLEWARE_CLASSES
.
質問:どのように適切にDjangoのV1.11でのミドルウェアクラスを定義するには?
[Alasdair](https://stackoverflow.com/users/113962/alasdair)、ありがとう。私は 'process_template_response'メソッドが古い' process_response'メソッドを置き換えるものだと考えました。 –