Data in 'context' is not passed to HTML

The context within the first conditional statement is delivered well, but the context updated within the second conditional statement is not delivered in HTML.

For example, 'MODEL_LIST' in first context is delivered well, but 'mape_val' in second context is not. I want it to be delivered.

How Can I solve this problem ?

<views.py function CODE>

def csv_forecast(request):

    context = {}
    username = request.user
    
    if request.method == 'POST' and request.FILES.get('csvfile'):
        uploaded_file = request.FILES.get('csvfile')

        p_data = pd.read_csv(uploaded_file)
        p_data.reset_index(drop=True, inplace=True)
        columns_list = list(p_data.columns)
        columns_list = [column.lower() for column in columns_list]
        p_data.columns = columns_list

        os.makedirs('media/csv', exist_ok=True)
        p_data.to_csv(f'media/csv/{username}.csv', index=False)

        start_date = p_data.loc[0, 'date']
        len_date = int(len(p_data)*0.8)
        end_date = p_data.loc[len_date, 'date']
        datas = [] 
        
        for i in range(1, len(columns_list)):
            datas.append(columns_list[i])
        
        MODEL_LIST = ['ARIMA', 'EMA5', 'LSTM']
        
        context = {'datas' : datas, 'd' : p_data, 'columns_list' : columns_list, 'MODEL_LIST' : MODEL_LIST,
        'start_date' : start_date, 'end_date' : end_date}
        

    if request.POST.get('sendModel') and request.POST.get('sendPdata') and request.POST.get('sendRdata'):
    
        send_pdata = request.POST.get('sendPdata')
        send_rdata = request.POST.get('sendRdata')
        send_model = request.POST.get('sendModel')
        cleaned_pdata = re.split(r'[\[\],"]', send_pdata)
        cleaned_rdata = re.split(r'[\[\],"]', send_rdata)
        cleaned_model = re.split(r'[\[\],"]', send_model)
        selected_pdata = [i for i in cleaned_pdata if len(i) >= 1]
        selected_rdata = [i for i in cleaned_rdata if len(i) >= 1]
        selected_model = [i for i in cleaned_model if len(i) >= 1]

        csv_data = pd.read_csv(f'media/csv/{username}.csv')
        mape_val, y_pred, y_test, test_date = model_main(csv_data, selected_pdata,selected_rdata, selected_model)
        mape_val = float(mape_val)
        print(mape_val)
        
        fs = FileSystemStorage(location=f'media/csv/')
        fs.delete(f'{username}.csv')
        
        context.update({'mape_val':mape_val, 'y_pred':y_pred, 'y_test':y_test, 'test_date':test_date})

    return render(request, 'csv_forecast.html', context)

HTML CODE PICTURE

HTML PAGE PICTURE

Debugging

Debug Console

Back to Top