0
在下面的示例代碼中聲明調度方法中的變量是否有效? 如果不是有更好的CBV編碼標準嗎?我的想法是避免重新聲明同一變量在每一個HTTP方法Django CBV調度方法的有效語法
class MyClsName(View):
template_name = "setup/code_install.html"
def dispatch(self, request, *args, **kwargs):
self.name = request.session['name']
self.obj = MyModel.objects.get(Name=self.name)
return super(MyClsName, self).dispatch(request, *args, **kwargs)
def get(self, request):
# obj = MyModel.objects.get(Name=self.name) --> to avoid
context = {'account': self.obj}
return render(request, self.template_name, context)
def post(self, request):
# obj = MyModel.objects.get(Name=self.name) --> to avoid
context = {'account': self.obj}
return render(request, self.template_name, context)
yup,在'dispatch'裏做的事情完全沒問題 – yedpodtrzitko