[Django]-To retrieve data from django database and display in table

4👍

I got the answer. It is working fine now.

Views.py:

def displaytrans(request):
    TransType.objects.all()
    info = TransType.objects.all()
    info2 = Trans.objects.all()
    print info
    bookdata = { "detail" : info, "details" : info2 }
    print bookdata
    resp =  render_to_response("account/displaytrans.html", bookdata, context_instance=Context(request))
    return resp

displaytrans.html:

<!doctype html>
<html>
  <body>

    <table border="1" style="width:800px">
      <tr>
        <td>  {% for s in details %} </td>
        <td>   {{ s.script }} </td>
      </tr>
      <tr> 
        <td>   {{ s.transtype_id}} </td>
        {% endfor %} 
      </tr>         
    </table>
  </body>
</html>

url.py:

url(r'^displaytrans/$', 'booki.account.views.displaytrans', name='displaytrans'),   

0👍

The traceback shows that the error is coming in your Trans model’s __unicode__ method. As the error says, you need to actually return unicode from that method, but you are returning the related TransType.

You could fix this by explicitly converting to unicode in that method:

return unicode(self.transtype)

but you should probably pick a better string representation of your data: there are going to be many Trans objects with the same TransType, so your unicode should distinguish between them, perhaps by also showing the script field.

Leave a comment