【问题标题】:Ajax not working on other model instance(object) in FOR LOOPAjax 不适用于 FOR LOOP 中的其他模型实例(对象)
【发布时间】:2015-07-29 05:23:09
【问题描述】:

我需要实现 AJAX 及其正常工作。但是,我在将 ajax 应用于模型的每个实例时遇到问题。它只是应用于顶部对象并在同一页面上应用 ajax 调用。对于剩余的对象,当我单击链接时,它会将我定向到新页面并向我显示我不想要的 JSON 对象。

Views.py

def upvote(request, post_id):

if request.method == "POST":
    p = get_object_or_404(Post, pk=post_id)
    p.upvote += 1
    p.save()
    return JsonResponse({'count':p.upvote })

if request.method == "GET":
    p = get_object_or_404(Post, pk=post_id)
    p.upvote += 1
    p.save()
    data = {
        'count' : p.upvote,
    }
    return JsonResponse({'count': p.upvote})

网址.py

url(r'^(?P<post_id>[0-9]+)/upvote/$', views.upvote, name='upvote-detail'),

Views.html

<script>
    $(function(){
       $('#up_vote').click(function(e) {
          e.preventDefault();
          window.alert("hello");
          console.log("hello");


          $.ajax({
            url: $(this).attr('href'),
            type :'get' ,
            success : function (data){
              // alert('success');
              $('#upvote_count').html(data.count);
            },
            failure : function (data){
              alert('failure') ; 
            }
          }) ;  // ajax call 

       }); // upvote link call
</script>

<div id="columns">
    {% for post in object_list %}

        <div class="pin">
            <a href="/posts/{{ post.id }}/">

            <img src= "/static/media/{{post.image}}" />
            </a>
            <p>
                {{post.description}}
                (<a href="{% url "post-edit" pk=post.id %}">edit</a>)
            </p>


            <div style="float:left">
              <a href='{% url 'upvote-detail' post.id %}' id='up_vote'>Up vote  </a>
              <span id='upvote_count'> {{ post.upvote }} </span> 
            </div>


            <div style="float:right">
              <a href='{% url 'downvote-detail' post.id %}' id='down_vote'> Down vote </a>
              <span id='downvote_count'>{{post.downvote}}</span>
            </div>


        </div>
    {% endfor %}

    </div>

这是我所有的文件。目前 AJAX 调用运行良好。它只是应用于 LOOP 中对象的顶部(第一个)。对于剩余对象,当我单击链接时,它会将我呈现到新页面。有人可以识别问题吗?谢谢,

【问题讨论】:

    标签: jquery python ajax django django-models


    【解决方案1】:

    问题是您的id 应该对每个 DOM 元素都是唯一的......请改用class

    <a href='{% url 'upvote-detail' post.id %}' class='up_vote'>Up vote  </a>
    
    $('a.up_vote').click(function(e) {...
    

    https://softwareengineering.stackexchange.com/questions/127178/two-html-elements-with-same-id-attribute-how-bad-is-it-really

    编辑 从 span 中删除 id 并在 ajax 成功时使用它

    $(this).siblings('span').html(data.count);
    

    【讨论】:

    • 谢谢,它解决了我的问题。有一些小问题。除了第一个标签之外,它不会更新其他跨度标签。原因可能是 Span 有一个 id。我把它变成了课堂,它的行为很奇怪。它更新所有帖子并将一个帖子合并到另一个帖子编号。有什么帮助。?我知道与 jquery 有一些关系,但我不完全确定
    • 非常感谢您的宝贵时间。我了解上面的 jquery 添加往往会做什么。但由于某种原因,它没有更新 span 标签。相反,它保持不变。
    • 这就是我的 ajax 的样子 $('a.up_vote').click(function(e) { e.preventDefault(); window.alert("hello"); console.log("你好"); $.ajax({ url: $(this).attr('href'), type :'get' , success : function (data){ // alert('success'); console.log( 'hello from success ajax') console.log(data.count); $(this).siblings('span').html(data.count); // $('#upvote_count').html(data.count ); },
    • 你能看一下吗。感谢你的宝贵时间。非常感谢您的时间和帮助。
    猜你喜欢
    • 2017-06-21
    • 1970-01-01
    • 2021-11-24
    • 2017-03-20
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 2018-09-08
    • 2015-11-02
    相关资源
    最近更新 更多