1

The goal is rendering data to html using django every 5 second, but the problem is the looping does not working. This is the code:

view.py

def form(request):
try:
    file_name = request.FILES['files'].name
    nama = "uploads/" + file_name
    seq_act, num_obsv, observer, list_obsv = pr.readexcel(nama,0)
    p, A, B = pr.make_initial(observer,num_obsv)
    p_lampau, A_lampau, B_lampau = pr.baum_welch(p, A, B, seq_act)

    while True:
        if len(temp)==0:
            break
        seq_act_test = list(temp)
        temp[:] = []

        real_seq, repair_seq = pr.process(p_lampau, A_lampau, B_lampau,
                                          seq_act_test, observer_test,
                                          num_obsv)
        return render(request,'main/form2.html',{'real_seq' : real_seq,
                                                 'repair_seq': repair_seq})
        time.sleep(5)

except:
    return redirect('/main/awal')

form2.html

<html>
<head>
    {% load staticfiles %}
    <link rel="stylesheet" type="text/css" href="{% static 'main/vis.css' %}" />
    <script type="text/javascript" src="{% static 'main/vis.js' %}"></script>
    <link href="{% static 'main/css/prism.css' %}" rel="stylesheet">
    <link href="{% static 'main/css/ghpages-materialize.css' %}" type="text/css" rel="stylesheet" media="screen,projection">
    <link href="http://fonts.googleapis.com/css?family=Inconsolata" rel="stylesheet" type="text/css">
    <link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <style type="text/css">
        #mynetwork {
            width: 100%;
            height: 600px;
            border: 1px solid lightgray;
        }
    </style>
</head>
</html>
<body>
    <div>
    <center>
        <b> {{ real_seq }}<br>
        <b>{{ repair_seq }}
    </center>
    </div>
</body>

Thanks for everybody who looking and helping to solve the problem.

kelly
  • 13
  • 2
  • Once you call `return`, the function is over and the loop is finished. – John Gordon May 25 '16 at 19:00
  • What you are trying to do is sometimes called 'server push', and it requires keeping the HTTP connection open while the browser renders a partial page. If you *must* do this, look at this [old SO post](http://stackoverflow.com/questions/4787530/does-django-have-a-way-to-open-a-http-long-poll-connection). Since it's every 5 seconds, I recommend changing things so that the *browser* polls the server. Much simpler. BTW, checkout the jQuery plugin [taconite](http://jquery.malsup.com/taconite/), it makes it trivial to have a backdoor conversation between browser and server. – Peter Rowell May 25 '16 at 19:20
  • yeah, what you want to do seems irrelevant with django views; django deals with backend logic, not browser actions. Using javascript and ajax call may be good choices. – Leonard2 May 26 '16 at 12:05

1 Answers1

0

If your goal is showing actual data, you can render page as usual (without loop) and reload page with JS:

<script>
    setTimeout(function() {
        window.location.reload()
    }, 5000);
</script>
karthikr
  • 97,368
  • 26
  • 197
  • 188