私はDjangoのクラスベースのビューを試していました。処理された情報を "ハンドラ"メソッドで使用できるように、request
という特定の情報を処理する単純なクラスベースのビューを作成しようとしています。クラスベースのビューを使用して情報を処理しますか?
私は、ドキュメントが何を言っているのか完全に理解しているようではなく、これがミックスイン、一般的なビューなどであるべきかどうかは不明です。私が作ると思っています。このようなクラス:
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})
上記のコードに行くための正しい方法です:これは誰かのようなビューを書くことができます
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'}
?これを行うためのより簡単な方法がありますか?これにより、上記の機能が別の一般的なビュー(組み込みの一般的なビューなど)で使用されることを防ぎますか?
もっと良い方法があると思われる場合は、回答を投稿してください。 – Umang