0

image I want to save json data to models. How can this possible. I tried using url but could not given proper result. Please suggest any method to resolve this. If i use get method then this will get the data that will show in command line but not in simplified method. I followed this https://ask.metafilter.com/154077/How-do-I-read-a-JSON-object-in-Python but this will also does not solve my problem.

    #My alert
    [{"BillNo":"4353","BillDetails":"my bill","Amount":"455"},{"BillNo":"4353","BillDetails":"my bill","Amount":"455"},{"BillNo":"4353","BillDetails":"my bill","Amount":"455"},{"BillNo":"4353","BillDetails":"my bill","Amount":"455"},{"BillNo":"4353","BillDetails":"my bill","Amount":"45"},{"BillNo":"43","BillDetails":"my bill","Amount":"600"}]

    <script>
    $("#btnjson").click(function () {
       var array1 = [];
         $("tbody tr").each(function () {
           
                        //var amount = $(this).find('td').eq(2).text().trim();
                        //if (!isNaN(amount) && parseFloat(amount) > 0) {
                            var firstTableData = {};
                            firstTableData.BillNo = $(this).find('td').eq(0).text().trim();
                            firstTableData.BillDetails = $(this).find('td').eq(1).text().trim();
                            firstTableData.Amount = $(this).find('td').eq(2).text().trim();
                            array1.push(firstTableData);
                        //}
                    });
        {
        jQuery.get( "/jsondata/", { data: array1 } );
         
           alert(JSON.stringify(array1));
        };

        //e.preventDefault();
       });
            });
    </script>
    Bill No: <input type="text" name="billno" id="billNo" autocomplete="off">
       Amount: <input type="text" name="amount2" id="amnt" pattern="[0-9]+" title="please enter amount" autocomplete="off">
       <br>
       Bill Details: <input type="text" id="billDetails" name="billdetails" style="margin: 1px 0px 20px; width: 850px; height: 42px;" autocomplete="off">
       <button type="button" id="btnAdd" class="button" value="Add">Add</button> <button type="button" id="btn2" class="button" value="Complete all entries">Complete All entries</button>
        <br><br>
       <table name="txt" id="tempResult" align="center">
         <thead>
          <tr>
         <th>Bill No</th>
         <th>Bill Details</th>
         <th>Amount</th>
         <th>Action</th>
          </tr>
         </thead>
         <tbody>
         </tbody>
         <tfoot>
        <tr>
          <td></td>
          <td></td>
          <td></td>
          <td>Total : <span id="total"></span></td>
        </tr>
          </tfoot>
        </table>
    <input id="btnjson" type="submit" value="ok"  /><br />
#views.py

@csrf_exempt
def jsdata(request):
 b_details = request.POST['']
 data = json.loads(list(array1))
 for billno in data():
  print(billno)

    #models

    class Mvouchar(models.Model):
     related = models.ForeignKey(Signs, on_delete=models.CASCADE, null=True, blank=True)
     bill_no = models.CharField(max_length=80, null=True, blank=True)
     bill_details = models.CharField(max_length=1000, null=True, blank=True)
     am = models.CharField(max_length=30, null=True, blank=True)
  • Possible duplicate of [parsing json fields in python](https://stackoverflow.com/questions/19573747/parsing-json-fields-in-python) – a_k_v Oct 22 '18 at 07:25

1 Answers1

0

b_details = request.POST[''] causing the problem . Because the key is not exist. You can change it to b_details = request.POST.get['']. It will return None if key is not present. You need to change the API call to POST request something like this

$.ajax({
        url: '/jsondata/',
        data: {
          'BillNo': $(this).find('td').eq(0).text().trim();
           ...
        },
        dataType: 'json',
        success: function (data) {
          if (data.is_taken) {
            alert("Message.");
          }
        }
      });

I have very little knowledge in AJAX and JQuery. I referred post How do I POST with jQuery/Ajax in Django? and https://simpleisbetterthancomplex.com/tutorial/2016/08/29/how-to-work-with-ajax-request-with-django.html.

a_k_v
  • 1,558
  • 7
  • 18