-4

I am trying to refresh the content of a table every few seconds in my HTML page using javascript. I keep getting 500 error when it tries to refresh the div, internal server error. Could someone enlighten the reason this is not working? I have used this: Refresh div using JQuery in Django while using the template system as a reference to what I was doing. The page loads perfectly the first time just fails to refresh.

Here is my code:

urls.py

url(r'^specialScoreboard/$', views.specialScoreboard.as_view(), name='specialScoreboard'),
url(r'^specialScoreboardDiv/$', views.specialScoreboardDiv , name='specialScoreboardDiv'),

views.py

class specialScoreboard(generic.ListView):
    template_name = 'CTF/specialScoreboard.html'
    context_object_name = 'teams'

    @method_decorator(login_required)
    @method_decorator(never_ever_cache)
    def dispatch(self, request, *args, **kwargs):
        if getAnyActiveGame and request.user.is_staff:
            return super(specialScoreboard, self).dispatch(request, *args, **kwargs)
        else:
            return HttpResponseRedirect(reverse('CTF:no_active_game'))

    def get_queryset(self):
         """
            ordering teams by score
        """
        game = getAnyActiveGame()
        teams = get_teams_from_game(game)
        return sorted(teams, key=lambda a: a.get_score(game), reverse=True)

def specialScoreboardDiv():
    game = getAnyActiveGame()
    teams = get_teams_from_game(game)
    sortedList = sorted(teams, key=lambda a: a.get_score(game), reverse=True)
    return render_to_response('CTF/specialscoreboardDiv.html' , {'sortedList' :sortedList})

scoreboardRefresh.js + scoreboardDiv.html

<script>
  var scoreboardURL = '{% url '
CTF: specialScoreboardDiv ' %}';

function refresh() {
  $.ajax({
    url: scoreboardURL,
    success: function(data) {
      $('#scoreboardDiv').html(data);
    }
  });
};
$(document).ready(function($) {
  refresh();
  setInterval("refresh()", 3000);
})

</script>
<div class="panel panel-info">
  <div class="panel-heading">Scoreboard</div>
  <div class="panel-body">

    <div class="table-striped">
      <table id="scoreboardDiv" class="table table-hover">
        <thead>
          <tr>
            <th>#</th>
            <th>Team Name</th>
            <th>Score</th>
          </tr>
        </thead>
        <tbody>
          {% for team in teams %}
          <tr>
            <td>{{forloop.counter}}</td>
            <td>{{team.name}}</td>
            <td>{{team|getScoreTeam}}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
  </div>
</div>

I can't seem to be able to format the error, here is a picture of it: https://i.sstatic.net/OU7Pj.png https://i.sstatic.net/eoFOC.png https://i.sstatic.net/U73ei.jpg

12
  • What does the error say? Commented Sep 11, 2016 at 8:10
  • What is going on with that scoreboardURL code at the start of your JS? It makes no sense at all. Commented Sep 11, 2016 at 8:10
  • Also, please fix the indentation in the views.py. Which methods are part of the class and which aren't? Commented Sep 11, 2016 at 8:11
  • 1
    Actually, looks like we need a text of server error. Commented Sep 11, 2016 at 8:16
  • I added a picture of the error, I can't seem to be able to format it, always looks unreadable. Commented Sep 11, 2016 at 8:21

1 Answer 1

0

Your django view takes no arguments, but usually django tries to pass request param into it. From the error in the screenshot you provided in comments, looks likе this is your problem.

I think you error will be fixed by making your view function take this argument:

def specialScoreboardDiv(request):
    game = getAnyActiveGame()
    ...
Sign up to request clarification or add additional context in comments.

3 Comments

It doesn't error anymore but doesn't update the table content either :(
So, next you need to find out where is the problem. Does your view return the html it supposed to return? Does your JS get correct html? Does success callback ever fired? And so on.
Here is a picture of the data: i.imgur.com/IptrwHd.png in the ajax function, it does return the html of the correct div but does not update it. Am I missing something? The html page I am sending is the specialScoreboardDiv.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.