我是Django的初学者,我开发了一个从表单中获取数据的类视图。使用这些数据我运行我的算法,我想用图表显示结果。因此,要将数据从视图发送到我使用APIview的模板。但我的问题是我无法从第一类获取数据并获取数据并运行算法并将结果发送到第二类API视图
class GeneticView(TemplateView):
template_name = 'Optimisation/genetics.html'
maxTest = [1,2,3,4]
def get(self, request):
form = GeneticsForm()
return render(request, self.template_name, {'form': form})
def post(self, request):
form = GeneticsForm(request.POST)
if form.is_valid():
NP = form.cleaned_data['NP']
NGEN = form.cleaned_data['NGEN']
CXPB = form.cleaned_data['CXPB']
MUTPB = form.cleaned_data['MUTPB']
genetic = Genetic(NP, CXPB, MUTPB, NGEN)
genetic.GeneticMethod()
#Get the information of the RUN
best_ind = genetic.get_best()
run_time = genetic.get_RunTime()
args = {
'form': form,
'best': best_ind,
'run': run_time,
'max': genetic.get_max(),
'min': genetic.get_min(),
'avg': genetic.get_avg(),
'std': genetic.get_std(),
}
return render(request, self.template_name, args)
API类:
class ChartData(APIView):
authentication_classes = []
permission_classes = []
def get(self, request, format=None):
data = {
'max': [20, 12, 3, 2, 13],
'min': [10, 2, 3, 8, 5],
}
return Response(data)
我想从第一次发送最大和最小 我刚用一些数据测试了API类
谢谢