2021年3月22日星期一

Dynamic dictionary keys in DJango table

I am absolutely new to Django need help. I have a dynamic list of dictionaries which I want to display in django table.

[{'index': 'Price', 'Price': 1.0}, {'index': 'Sale', 'Price': 1.0}, {'index': 'Reviews', 'Price': -1.0}]  

"index" key will remain constant but second key is dynamic. I want to show this data in a table. I can show the value of first key but cant figure out how to show the value of dynamic key.

My django template code is as below

 <div class="container fluid">              <table class="table table-striped">                  <h2 class="text-center">Correlation with </u></h2><br>                  <thead>                      <tr>                          <th>Index</th>                          <th>Score</th>                                                  </tr>                                        </thead>                  </tbable>          </div>  

Views.py

def corelation(request):      if request.method == "POST":          var_input = request.POST.get('variable')          data = {'Price':[23, 65], 'Sale':[76, 82], 'Reviews':[52, 34]}          df = pd.dataframe(data)          corr = df.corr()          var_corr = corr[var_input].sort_values(ascending = False)                      #this is for showing item wise correlation() fucntion          json_varCorr = var_corr.reset_index().to_json(orient ='records')          data_varCorr = []          data_varCorr = json.loads(json_varCorr)            context = {'variable': data_varCorr,                      'input':var_input,          }                    return render(request, 'variable.html', context)  
https://stackoverflow.com/questions/66757209/dynamic-dictionary-keys-in-django-table March 23, 2021 at 12:17PM

没有评论:

发表评论