Dynamic page refreash

20 views
Skip to first unread message

lmcadory

unread,
Aug 2, 2011, 1:22:46 PM8/2/11
to Django users
I'm having this problem, my web design knowledge is limited and I'm
having a hard time searching for the solution (if there is one)

The problem is this:

I have a view that counts the number of lines in a file. It then
pushes that result to an html page. What I want it to do is when that
line count changes when I press the refresh button on the browser it
displays the new number. Right now I have to reload the server to
display the results.

I, if I'm asking the question correctly, is there a way I can tell the
view to run again and republish the results?

bruno desthuilliers

unread,
Aug 2, 2011, 1:37:35 PM8/2/11
to Django users
On 2 août, 19:22, lmcadory <ljmcad...@gmail.com> wrote:
> I'm having this problem, my web design knowledge is limited and I'm
> having a hard time searching for the solution (if there is one)
>
> The problem is this:
>
> I have a view that counts the number of lines in a file. It then
> pushes that result to an html page.

Uh ? What does that mean exactly ? (the "pushes that result to an html
page" part)

> What I want it to do is when that
> line count changes when I press the refresh button on the browser it
> displays the new number. Right now I have to reload the server to
> display the results.

You shouldn't, so chances are you did something wrong (like putting
the code that computes the lines count at the module top-level or
something alike), but it's hard to tell exactly without looking at
your code.

>
> I, if I'm asking the question correctly, is there a way I can tell the
> view to run again and republish the results?

The view function is called each time you reload the corresponding
url.

lmcadory

unread,
Aug 2, 2011, 1:55:49 PM8/2/11
to Django users
Here is my code, minus all the import statements.

file = 'someFile.txt'
infile = open(file, 'r')

def test_results(request):
expectedResults = 10
lines = infile.readlines()
lineCount = len(lines)
if lineCount == expectedResults:
p = 'Passed'
return render_to_response('results.html', {'results':p}
else:
f = 'Fail'
return render_to_response('results.html', {'results':f}



#HTML
....
<body>
The test {{ results }}.
</body>
....

lmcadory

unread,
Aug 2, 2011, 1:57:16 PM8/2/11
to Django users
Display the results.

On Aug 2, 1:37 pm, bruno desthuilliers <bruno.desthuilli...@gmail.com>
wrote:

lmcadory

unread,
Aug 2, 2011, 1:58:42 PM8/2/11
to Django users
There should be closing brackets on the ends of the 2
render_to_response statements.

lmcadory

unread,
Aug 2, 2011, 3:43:25 PM8/2/11
to django...@googlegroups.com
I fixed it. I needed to remove --noreload

dirleyrls

unread,
Aug 2, 2011, 4:54:38 PM8/2/11
to Django users
I think you should open, count the lines of and close the file inside
the view. The view code is executed once per request. I think that
somethink like this will solve your problems:

def test_results(request):
infile = open('someFile.txt', 'r')
# ... count the lines of the file
infile.close()
return render_to_response('results.html', {'results':f}



bruno desthuilliers

unread,
Aug 2, 2011, 5:57:26 PM8/2/11
to Django users
On 2 août, 21:43, lmcadory <ljmcad...@gmail.com> wrote:
> I fixed it. I needed to remove --noreload

This doesn't fix anything - try to run it using anything but the test
server and you'll have the same problem (or even worse in a multi-
threading or multi-process environment).

You actually have to move the first two lines :

file = 'someFile.txt'
infile = open(file, 'r')

within your view function. Else the file is only opened when your
view module is imported, not when the view function is called.

Also, beware that if you don't use an absolute path for the file, it
will be looked for in the "current working directory" whatever it is
at that time (warning: the "current working directory" is NOT your
app's or project directory - it can be absolutely any possible
directory on your filesystem).

H.İbrahim Yılmaz

unread,
Aug 2, 2011, 5:58:04 PM8/2/11
to django...@googlegroups.com
http://www.quackit.com/javascript/javascript_refresh_page.cfm

2011/8/2 lmcadory <ljmc...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.




--
http://www.arkeoloji.web.tr
Reply all
Reply to author
Forward
0 new messages