Reading from a file and displaying into a table on the web app.

35 views
Skip to first unread message

Ravi Hemnani

unread,
Apr 24, 2014, 10:46:01 AM4/24/14
to django...@googlegroups.com
Hey,

I am trying to build an interface using django. This way i am learning django as well.

So i have completed web-poll tutorial and i tried something on my own.

My problem, 

I have a file with following content.

# Stats

total_connections_received:2

total_commands_processed:1

instantaneous_ops_per_sec:0

rejected_connections:0

expired_keys:0

evicted_keys:0

keyspace_hits:0

keyspace_misses:0

pubsub_channels:0

pubsub_patterns:0

latest_fork_usec:0

# Keyspace

db0:keys=6,expires=0




Now, i want to display the above content in a table from on the web interface.

Following is my directory structre where mysite is the root app and polls is the secondary app.

ravi@ravi-ThinkPad-X230:~/python/mysite$ tree
.
├── manage.py
├── mysite
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── settings.py
│   ├── settings.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── polls
    ├── admin.py
    ├── admin.pyc
    ├── __init__.py
    ├── __init__.pyc
    ├── models.py
    ├── models.pyc
    ├── static
    │   └── polls
    │       ├── images
    │       │   └── images.jpg
    │       └── style.css
    ├── templates
    │   └── polls
    │       ├── detail.html
    │       ├── index.html
    │       ├── results.html
    │       └── table.html
    ├── test.py
    ├── test.pyc
    ├── tests.py
    ├── tests.pyc
    ├── urls.py
    ├── urls.pyc
    ├── views.py
    └── views.pyc

7 directories, 29 files

following is my polls/templates/polls/table.html

{% if line %}
    <table style="width:300px">
    {% for x in line  %}
        <tr>
            <td>{{ x }}</td>
        </tr>
    {% endfor %}
    </table>
{% else %}
    <p>No polls are available.</p>
{% endif %}

following is my polls/views.py

from django.shortcuts import get_object_or_404, render
from django.http import Http404, HttpResponse, HttpResponseRedirect
from polls.models import Choice, Poll
from django.core.urlresolvers import reverse
from django.template import RequestContext, loader
from django.views import generic
from django.core.files import File

def table(request):
        with open('/home/ravi/python/temp.txt', 'r') as f:
                for line in f:
                        context = {'line': line}
                return render(request, 'polls/table.html', context)


when it the url, 


I get the following output,

d
b
0
:
k
e
y
s
=
6
,
e
x
p
i
r
e
s
=
0

but i want the output as each line of the file as a row in the table. 

Any help. 

Erik Cederstrand

unread,
Apr 24, 2014, 10:53:29 AM4/24/14
to Django Users

Den 24/04/2014 kl. 16.46 skrev Ravi Hemnani <raviii...@gmail.com>:

> def table(request):
> with open('/home/ravi/python/temp.txt', 'r') as f:
> for line in f:
> context = {'line': line}

Here you are ovwewriting 'content' on every iteration. When the loop exits, only the last line in the file will be in context. You want something like this instead:

def table(request):
with open('/home/ravi/python/temp.txt', 'r') as f:
context = {'lines': f.readlines()}


Erik

Ravi Hemnani

unread,
Apr 28, 2014, 4:15:57 AM4/28/14
to django...@googlegroups.com
Hey, 

Sorry to reply so late. I figured that thing out and i was able to send the results into a table successfully. 

Thanks for the help :D
Reply all
Reply to author
Forward
0 new messages