pythonのdjangoにおいて、class間で変数の値の共有をしたいです。
python
1from django.shortcuts import render 2from django.views.generic import FormView 3from . import forms 4import math 5class BodyView(FormView): 6 form_class = forms.Dietform 7 template_name = "mens_diet/index.html" 8 9 def form_valid(self, form): 10 data = form.cleaned_data 11 weight = data["weight"] 12 height = data["height"] 13 yourage = data["yourage"] 14 15 weight_math = int(weight) 16 height_math = int(height) 17 yourage_math = int(yourage) 18 19 bmr = (13.397 * weight_math) + (4.799 * height_math) - (5.677 * yourage_math) +88.362 20 BMR = math.floor(bmr) 21 ctx = self.get_context_data(form=form, BMR=BMR) 22 return self.render_to_response(ctx) 23 24 25 26class LifeView(FormView): 27 28 29 def __init__(self,a=BodyView): 30 self.params = { 31 'form':forms.LifeForm, 32 'result': None, 33 'BMR': a.BMR, 34 } 35 36 37 def get(self,request): 38 return render(request,'mens_diet/life.html',self.params) 39 40 def post(self, request, BMR): 41 ch = request.POST['choice'] 42 if ch == 'one': 43 math_result = BMR * 3 44 else: 45 math_result = BMR * 0 46 self.params['result'] = math_result 47 48 self.params['form'] = forms.LifeForm(request.POST) 49 return render(request, 'mens_diet/life.html',self.params) 50 51 52 53 54 55 56 57 58
前提・実現したいこと
pythonのdjangoのviews.py において、他のクラスで生成したclassの変数の値を利用したいです。
### 補足情報
回答2件
あなたの回答
tips
プレビュー