How to append values in a tabular format from JSON in Django Template?

此生再无相见时 提交于 2020-03-25 13:48:44

问题


I am making a small web application that allow user to perform OCR on the selected image and provides the JSON output in a tabular Format like this.

JSON DATA :

{
  "data": {
    "test_1": {
      "test_name": "FASTING SUGAR", 
      "results": "121.00", 
      "units": "mg%", 
      "low_range": 70.0, 
      "high_range": 110.0
    }
  }
}

What I'm facing now is when I'm selected new image to add values in the table, it overrides the values instead of appending.

And what I want is to append new value in the table by choosing another image, the previous row show me stay as it is and new will get append.

So that finally I can submit all the values to the database all together by clicking on the submit button.

Here is my template code :

<form method="post">{% csrf_token %}
  <div class="table-responsive">
    <table id="datatable2" class="table order-column hover">
      <thead>
        <tr>
          <th>Investigation Name</th>
          <th>Result</th>
          <th>UOM</th>
          <th>Low Range</th>
          <th>High Range</th>
        </tr>
      </thead>
      <tbody>
        {% for key, value in data.items %}
        {% for key2,value2 in value.items %}
        <tr class="gradeX">
        <td>{{ value2.test_name }}</td>
        <td>{{ value2.results }}</td>
        <td>{{ value2.units }}</td>
        <td>{{ value2.low_range }}</td>
        <td>{{ value2.high_range }}</td>
      </tr>
    {% endfor %}
    {% endfor %}
  </tbody>
</table>
</div>
<!--end .table-responsive -->
<button type="submit" class="btn btn-primary" name="data">Submit</button>
</form>

And my views.py

from django.shortcuts import render, redirect
import json
import urllib.request
from .models import Panel
from django.contrib import messages
from .forms import PanelForm, SingleDataForm

def photo_single(request):
    if request.method=='POST':
        form = PanelForm(request.POST, request.FILES)
        form1 = SingleDataForm()
        if form.is_valid():
            print("valid")
            temp,data = form.save()
            return render(request, 'album/photo_single.html', {'data': data, 'form1':form1, 'form': form})
            #return redirect('photo_list')
    else:
        form = PanelForm()

    if request.method=='POST' and 'data' in request.POST:
        form1 = SingleDataForm(request.POST)
        if form1.is_valid():
            print("INside this loop")
            if form1.save():
                print("INside this loop")
                return redirect('album/photo_single.html', messages.success(request, 'Order was successfully created.', 'alert-success'))
            else:
                return redirect('album/photo_single.html', messages.error(request, 'Data is not saved', 'alert-danger'))
        else:
            return redirect('album/photo_single.html', messages.error(request, 'Form is not valid', 'alert-danger'))
    else:
        form1 = SingleDataForm()
    return render(request, 'album/photo_single.html', {'form': form, 'form1':form1})

How can we use list in DTL to store the previous values and stay as it is and the new value just get append in the next row.. ? I'm new in Django. Please Guide. Thanks.

来源:https://stackoverflow.com/questions/60734026/how-to-append-values-in-a-tabular-format-from-json-in-django-template

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!