'displaying json data to django template

i am getting data from mongodb and first inserting all the data into pandas and then converting that data to json data and then print that data in table format my views.py looks like this

def generate(request):
    a=str(request.POST.get('level'))
    b= request.POST.get('status')
    c=(request.POST.get('startdate'))
    g=datetime.strptime(c,"%Y-%d-%m")
    d=datetime.strftime(g,"%Y/%d/%m")
    e=request.POST.get('enddate')
    h=datetime.strptime(e,"%Y-%d-%m")
    f=datetime.strftime(h,"%Y/%d/%m")
       


    output=run([sys.executable,'C:\\Users\\siddhant\\Desktop\\intern mongo\\indicator\\work\\water.py',a,b,d,f],stdout=PIPE,text=True)
    
    client=pymongo.MongoClient("mongodb://localhost:27017")
    db = client["water"]
    colle= db["waterlevel"]
    df3=pd.DataFrame()
    df4=pd.DataFrame()
    data_from_db = colle.find({},{"_id":0,"time":1,"status":1,"level":1})
    for datta in data_from_db:
      df=pd.DataFrame(datta)
      df4=pd.concat([df4, df], ignore_index=True,axis=0)
    
    
 
   
    
    
    json_records = df4.reset_index().to_json(orient='records',date_format='epoch')
    data = []
    data = json.loads(json_records)
    context = {'d': data}
    

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

and my html template is this


<!DOCTYPE html>
<html lang="en">
<head>
  <title>TableView - Startup</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
</head>
<body>
  
<div class="container">
  <h2 class="text-center"><u>water table</u></h2><br>            
  <table class="table table-dark table-striped">
    <thead>
      <tr>
        <th>time</th>
        <th>status</th>
        <th>level</th>
       
      </tr>
    </thead>
    <tbody>
    <!-- jinja2 Technique -->
    {% if d %}  
    {% now "U" %}
    {% for i in d %}
      <tr>
        <td>{{i.time|date:'U'}}</td>
        <td>{{i.status}}</td>
        <td>{{i.level}}</td>
      </tr>
    {% endfor %}
    {% endif %}
    </tbody>
  </table>
</div>
  
</body>
</html>

the problem is I am getting DateTime in UNIX format I want it in readable date type format can someone suggest a better way to display pandas dataframe in Django template as a table other than to_html function and also please suggest how can i JSON date format to readable date format



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source