【问题标题】:Update a variable every 5 second using jquery使用 jquery 每 5 秒更新一次变量
【发布时间】:2020-01-13 20:46:04
【问题描述】:

我正在使用 django 创建网页,这是我第一次这样做。我试图以 5 秒的间隔从 .py 文件中获取变量的值。下面是 HTML 代码:

<!DOCTYPE html>
    <html>
    <head>
        <title>Like Post App</title>

    <script
      src="https://code.jquery.com/jquery-3.4.1.js"
      integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
      crossorigin="anonymous"></script>

      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</head>
<body>
    <div class = "display-3 color-red"><center>DataFlair AJAX Tutorial<br>Post APP</center></div>
    {% for post in posts %}
        <div class = 'container jumbotron'>
        <h3 class="display-5">{{ forloop.counter }}. {{ post.post_heading }}</h3>
<p class="lead">{{ post.post_text }} </p>
        <p>
        <div type="text/css" class = "container">Author : {{ post.post_author }}</div>
        <a class="likebutton btn btn-primary btn-lg" id="like{{ post.id }}" data-catid="{{ post.id }}">Like({{ post.like_ref.counter }})</a> </p> <p id="message{{post.id}}">
</p>
</div>
{% endfor %}

<script type="text/javascript">

              setInterval(function() {
                    getvalue(); // Do something every 5 seconds
              }, 5000);

getvalue();
              function getvalue(){

                      var id;
                      id = $(this).attr("data-catid");
                      $.ajax(
                        {
                          type:"GET",
                          url: "like",
                          data:{
                             post_id: id
                                },
                      success: function( data )
                      {
                          $( '#like'+ id ).text("Like(" + data +")");
                          var i=parseInt(data);
                          console.log("Value= "+i);
                      }
                    }
                  )
                }


</script>
</body>
</html>

下面是views.py代码:

import json
from django.shortcuts import render
from .models import Post, Like
from django.http import HttpResponse
# Create your views here.
#DataFlair #AJAX_tutorial
def index(request):
    posts = Post.objects.all()
    return render(request, 'post/index.html', { 'posts': posts })
def like(request):
    if request.method == 'GET':
        post_id = request.GET['post_id']
        likedpost = Post.objects.get(id = post_id )
        m = Like.objects.filter( post=likedpost ).first()
        m.counter +=1
        m.save()
        value1= int(m.counter)
        #data1= {'cmd': 'success', 'ctr': str(m.counter) }
        return HttpResponse(value1)
        #return HttpResponse(json.dumps(data1))


    else:
        return HttpResponse("unsuccesful")

我不断收到以下错误: 1) GET http://localhost:8000/ajax/like/ 500 (内部服务器错误) 2) GET http://localhost:8000/favicon.ico 404 (未找到)

请帮忙。

【问题讨论】:

  • favicon.ico 错误与您的问题无关。许多网站都有这个错误,因为浏览器会自动尝试下载favicon.ico 以便能够在浏览器选项卡中显示它。 “500 internal server error”表示您的 python 脚本有错误。你能发布更多细节吗?
  • 检查您的服务器日志以了解 500 错误的原因。

标签: python jquery html django ajax


【解决方案1】:

我相信:

id = $(this).attr("data-catid");

没有指向按钮...也许你应该尝试:

id = $(".likebutton").attr("data-catid");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 2014-10-16
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多