class SupervisionView(MyBaseView, TemplateView):
template_name = 'research/a1.html'
def get_context_data(self, **kwargs):
context = super(SupervisionView, self).get_context_data(**kwargs)
context['supervisions'] = list1
return context
def post(self, request, *args, **kwargs):
if 'confirm_supervision1' in request.POST:
return redirect(reverse_lazy('t_app:dept1', kwargs={'year': self.kwargs['year']}))
class SupervisionView2(MyBaseView, TemplateView):
template_name = 'research/a2.html'
def get_context_data(self, **kwargs):
context = super(SupervisionView2, self).get_context_data(**kwargs)
context['supervisions'] = list 2
return context
def post(self, request, *args, **kwargs):
if 'confirm_supervision2' in request.POST:
return redirect(reverse_lazy('t_app:dept2', kwargs={'year': self.kwargs['year']}))
나는 같은 일을 다시하는 약 20 개의 홀수 함수가있다. 컨텍스트 변수 만 변경하고 각보기에서 URL을 리디렉션합니다. 이것을 압축하는 가장 좋은 방법은 무엇입니까?django에서 비슷한 종류의 뷰 재사용
class MyDRYMixin(object):
context_variable = None
post_variable = None
redirect_name = None
def get_context_data(self, **kwargs):
context = super(MyDRYMixin, self).get_context_data(**kwargs)
if self.context_variable is not None:
context['supervisions'] = self.context_variable
return context
def post(self, request, *args, **kwargs):
if self.post_variable is not None and self.post_variable in request.POST:
return redirect(reverse_lazy(self.redirect_name, kwargs={'year':self.kwargs['year']}
그런 다음이 세 가지 변수를 정의해야하고, 귀하의 의견에 그 믹스 인을 사용합니다 :
, 나는 그것을 해결하고 –
늦게 응답 실례 업데이트된다. 접근 방식은 동적 인 가치가 필요하다는 것만이 유일합니다. 내 말은, supervisionView 'context_variable'대신 'list1'이 필요합니다. request.user 또는 다른 곳에서 메소드를 호출해야합니다. 어떻게 할 수 있습니까? –
나는 내 대답에 도움이 될만한 정보를 편집했습니다. 내가 무엇을 명확히 할 수 있는지 또는 어떤 식 으로든 당신이 묻는 것을 오해 한 경우 알려주십시오. 감사! 당신이 게시처럼 – souldeux