Common task for showing data could be to load a comma/Tab or any other character seperated file and display in table on webpage. this Could be achieved by simple Process. First Step is to design Template.
Here is html code with django template tags to be used as template. Remember here I have used Simple html and django template tags which will be rendered at the time of execution. if you need any information on how these tags works let you ask them in comments.
{tbr.html}
<html>
<head>
<title>
My Test Django App
</title>
</head>
<body>
<table style = "width:600px;background-color:#888888;" border = "2">
<tr>
<td>Header1</td>
<td>Code</td>
<td>Vendor</td>
<td>Destination</td>
<td>Rate</td>
<td>Effective Date</td>
</tr>
{% for line in lines %}
<tr>
{% for rec in line %}
<td>{{rec}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>
Then in View.py Render From function called by URL and Define another function to read file.
from django.core.context_processors import csrf
from django.http import HttpResponse
from django.template import Template, loader, Context
import os;
def index(request):
lines = ReadTBR()
t = loader.get_template('Templates\\tbr.html')
c = Context({"user":request.user, "lines":lines})
c.update(csrf(request))
return HttpResponse(t.render(c))
def ReadTBR():
path = os.path.join('.','../tbr.out')
tbr = open(path,"r")
res = []
for line in tbr.readlines():
l = line.split('\t')
rec = [l[0],l[1],l[2],l[3],l[4],l[5]]
res.append(rec)
return res
Here is html code with django template tags to be used as template. Remember here I have used Simple html and django template tags which will be rendered at the time of execution. if you need any information on how these tags works let you ask them in comments.
{tbr.html}
<html>
<head>
<title>
My Test Django App
</title>
</head>
<body>
<table style = "width:600px;background-color:#888888;" border = "2">
<tr>
<td>Header1</td>
<td>Code</td>
<td>Vendor</td>
<td>Destination</td>
<td>Rate</td>
<td>Effective Date</td>
</tr>
{% for line in lines %}
<tr>
{% for rec in line %}
<td>{{rec}}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</body>
</html>
Then in View.py Render From function called by URL and Define another function to read file.
from django.core.context_processors import csrf
from django.http import HttpResponse
from django.template import Template, loader, Context
import os;
def index(request):
lines = ReadTBR()
t = loader.get_template('Templates\\tbr.html')
c = Context({"user":request.user, "lines":lines})
c.update(csrf(request))
return HttpResponse(t.render(c))
def ReadTBR():
path = os.path.join('.','../tbr.out')
tbr = open(path,"r")
res = []
for line in tbr.readlines():
l = line.split('\t')
rec = [l[0],l[1],l[2],l[3],l[4],l[5]]
res.append(rec)
return res
No comments:
Post a Comment