使用javascript

时间:2016-09-11 08:07:44

标签: javascript jquery python html django

我正在尝试使用javascript在HTML页面中每隔几秒刷新一次表的内容。当它尝试刷新div,内部服务器错误时,我一直收到500错误。有人可以启发这个不起作用的原因吗?我用过这个:Refresh div using JQuery in Django while using the template system  作为我正在做的事情的参考。第一次无法刷新时页面加载完美。

这是我的代码:

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>
&#13;
<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>
&#13;
&#13;
&#13;

我似乎无法格式化错误,以下是它的图片:http://i.imgur.com/Yc11juA.png http://i.imgur.com/QluqZyc.png http://imgur.com/QluqZyc

1 个答案:

答案 0 :(得分:0)

您的django视图不带参数,但通常django会尝试将请求参数传递给它。从您在评论中提供的屏幕截图中的错误,看起来这是你的问题。

我认为你的错误将通过让你的视图函数采用这个参数来解决:

def specialScoreboardDiv(request):
    game = getAnyActiveGame()
    ...