Python Upvote操作正在对所有卡django执行 我正在为我的网站实施向上投票/向下投票系统。但是,在对某个特定项目进行升级投票时,所有项目都在进行升级投票 htmlcode

Python Upvote操作正在对所有卡django执行 我正在为我的网站实施向上投票/向下投票系统。但是,在对某个特定项目进行升级投票时,所有项目都在进行升级投票 htmlcode,python,django,Python,Django,您可以只使用一个脚本,而不是创建多个脚本,用于DOM中的所有表单。您只需将action=“{%url'upvote'pk=marking.id%}”添加到表单标记中,这将为您提供更新记录的路径和id。另外,使用类代替id。然后,使用$(this.attr(“action”)获取此操作值,并将其用作ajax调用中的url 演示代码: $(文档).on('submit','post invest',函数(e){ e、 预防默认值(); var comment=“upvote”; console.l

您可以只使用一个脚本,而不是创建多个脚本,用于
DOM
中的所有表单。您只需将
action=“{%url'upvote'pk=marking.id%}”
添加到表单标记中,这将为您提供更新记录的路径和id。另外,使用代替id。然后,使用
$(this.attr(“action”)
获取此操作值,并将其用作ajax调用中的
url

演示代码

$(文档).on('submit','post invest',函数(e){
e、 预防默认值();
var comment=“upvote”;
console.log($(this.attr(“操作”))
$.ajax({
键入:“POST”,
url:$(this.attr(“action”),//在此处传递操作
数据:{
评论:评论,,
csrfmiddlewaretoken:“{{csrf_令牌}}”,
行动:"邮政"
},
成功:函数(xhr、errmsg、err){
window.location=“品牌”
},
错误:函数(xhr、errmsg、err){
$('#results').html(“哎呀!我们遇到了一个错误:”+errmsg+
" ");
}
});
});

mAbc
1.

随机文本


查看更多 
XYZ 2.

随机文本


查看更多 
<div  style="margin-left: 10px;margin-right: 10px;">

  <div class="card-columns" >
    {% for marking in marking_maestro %}
    <div class="card ">
      <img class="card-img-top" src=""  alt="Card image cap">
      <div class="card-body">
        <h5 class="card-title" name="title">{{marking.product_title}}</h5>
        <h5 class="card-title" name="title">{{marking.id}}</h5>
        <p class="card-text">Random text</p>
        <br>

        <div  class="row" >
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target=".bd-modal-lg-1">View more</button>
        &emsp;
        <a href="{% url 'invest' pk=marking.id %}";><button type="submit" class="btn btn-primary" data-toggle="modal" >Invest</button></a>
      </div>
      <br>
      <form method="POST" id="post-invest">
        {% csrf_token %}
        <div  class="row" >
            <input class="btn btn-outline-secondary" id="upvote_button" type="submit" name="upvote" value="upvote">
        </div>
    </form>
    </div>
    <!-- for upvote -->
    <script>
      // upvote
  $(document).on('submit', '#post-invest',function(e){
      e.preventDefault();
      // amount = $('input[name="amount"]').val();
      console.log("Upvote initiated=");
      var comment = "upvote";
      $.ajax({
          type:'POST',
          url:"{% url 'upvote' pk=marking.id %}",
          data:{
              comment : comment,
              csrfmiddlewaretoken: '{{ csrf_token }}',
              action: 'post'
          },
          success: function(xhr, errmsg, err){
            window.location = "brand"
          },
          error : function(xhr,errmsg,err) {
          $('#results').html("<div class='alert-box alert radius' data-alert>Oops! We have encountered an error: "+errmsg+
              " <a href='#' class='close'>&times;</a></div>");
          console.log(xhr.status + ": " + xhr.responseText); 
      }
      });
  });
    </script>
   </div>
    <div class="modal fade bd-modal-lg-1" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
    
              <center><h4>Title</h4></center>
              <img class="img-fluid" src="{% static 'assets/img/about/about.png' %}">
              <br>
              <center><h4>Branding Video</h4></center>
    
              <div class="embed-responsive embed-responsive-16by9">
              <iframe class="rembed-responsive-item"  src="https://www.youtube.com/embed/UmljXZIypDc" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
              </div>
              <br>
              <button type="button" class="btn btn-secondary center" style="width: 200px;margin-left: auto;margin-right: auto;" data-dismiss="modal">Close</button>
    
    
              <br>
    
            </div>
          </div>
        </div>
        <div class="modal fade" id="bd-modal-lg-invest" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
          <div class="modal-dialog modal-dialog-centered" role="document">
            <div class="modal-content">
              <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLongTitle">Event name - {{id.product_title}} </h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                  <span aria-hidden="true">&times;</span>
                </button>
              </div>
            </div>
          </div>
        </div>
    {% endfor %}
</div>
def upvote(request, pk):
    # getting the brand info
    event_data = MarketingMaestro.objects.get(pk = pk)
    # getting name of the particular id
    event_name = event_data.product_title
    print("Printing event name=", event_name)

    # upvote comment in the db
    if request.POST.get('action') == 'post':
        response_data = {}
        name = request.user.username
        emailID = request.user.email

        # getting user details
        response_data['name'] = name
        response_data['emailID'] = emailID
        comment = 'upvote'

        # creating the value
        BrandVotes.objects.create(
            name = name,
            emailID = emailID,
            product_title = event_name,
            comment = comment
         )
    return JsonResponse(
                {
                    'message': "success"
                }
    )