私はDjangoのクラスベースのビューを試してrequest
いて、処理された情報を「ハンドラー」メソッドで使用できるように、特定の情報を処理する単純なクラスベースのビューを作成しようとしています。
ドキュメントの内容を完全に理解していないようで、これがMixinなのか、一般的なビューなのか、それとも他の何かなのかわからない。私はこのようなクラスを作ることを考えています:
class MyNewGenericView(View):
redirect_on_error = 'home'
error_message = 'There was an error doing XYZ'
def dispatch(self, request, *args, **kwargs):
try:
self.process_information(request)
# self.process_information2(request)
# self.process_information3(request)
# etc...
except ValueError:
messages.error(request, self.error_message)
return redirect(self.redirect_on_error)
return super(MyNewGenericView, self).dispatch(request, *args, **kwargs)
def process_information(self, request):
# Use get/post information and process it using
# different models, APIs, etc.
self.useful_information1 = 'abc'
self.useful_information2 = 'xyz'
def get_extra_info(self):
# Get some extra information on something
return {'foo':'bar'}
これにより、誰かが次のようなビューを作成できるようになります。
class MyViewDoesRealWork(MyNewGenericView):
def get(self, request, some_info):
return render(request, 'some_template.html',
{'info':self.useful_information1})
def post(self, request, some_info):
# Store some information, maybe using get_extra_info
return render(request, 'some_template.html',
{'info':self.useful_information1})
上記のコードは正しい方法ですか?これを行うためのより簡単でより良い方法はありますか?これにより、上記の機能が別の汎用ビュー(組み込みの汎用ビューなど)で使用されなくなりますか?