程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了显示回复按钮对评论不起作用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决显示回复按钮对评论不起作用?

开发过程中遇到显示回复按钮对评论不起作用的问题如何解决?下面主要结合日常开发的经验,给出你关于显示回复按钮对评论不起作用的解决方法建议,希望对你解决显示回复按钮对评论不起作用有所启发或帮助;

显示回复按钮应该针对不同的评论。当我按下显示回复按钮时,它会打开不属于该评论的不同回复。

  1. vIEws.py
@H_502_7@from django.shortcuts import render,httpResponse,redirect,reverse
from fitness.models import Post,BlogComment
from django.contrib.auth.models import User
from fitness.templateTags import extras

# Create your vIEws here.
def fitness(request):
    everypost=Post.objects.all()
    context={"everypost":everypost}
    return render(request,"fitness/fit.HTML",context)


def blogfit(request,slug):
    post=Post.objects.filter(slug=slug).first()
    comments= BlogComment.objects.filter(post=post,parent=None)
    replIEs= BlogComment.objects.filter(post=post).exclude(parent=None)
    replyDict={}
    for reply in replIEs:
        if reply.parent.sno not in replyDict.keys():
            replyDict[reply.parent.sno]=[reply]
        else:
            replyDict[reply.parent.sno].append(reply)

    context={"post":post,'comments': comments,'user': request.user,'replyDict': replyDict}
    return render(request,"fitness/blogfit.HTML",context)

def postComment(request):
    if request.method == "POST":
        comment=request.POST.get('comment')
        user=request.user
        postsno =request.POST.get('postsno')
        post= Post.objects.get(sno=postsno)
        parentSno= request.POST.get('parentSno')
        if parentSno=="":
            comment=BlogComment(comment= comment,user=user,post=post)
            comment.save()

        else:
            parent= BlogComment.objects.get(sno=parentSno)
            comment=BlogComment(comment= comment,post=post,parent=parent)
            comment.save()
    return httpResponse(reverse('fitness:fitness'))
  1. 模型.py
@H_502_7@from django.db import models
from ckeditor.fIElds import RichTextFIEld
from django.contrib.auth.models import User
from django.utils.timezone import Now
# Create your models here.

class Post(models.Model):
    sno=models.autoFIEld(primary_key=True)
    Title=models.CharFIEld(max_length=255)
    author=models.CharFIEld(max_length=14)
    slug=models.CharFIEld(max_length=130)
    timeStamp=models.DateTimeFIEld(blank=True)
    content=RichTextFIEld(blank=True,null=True)

    def __str__(self):
        return self.Title + " by " + self.author
class BlogComment(models.Model):
    sno= models.autoFIEld(primary_key=True)
    comment=models.TextFIEld()
    user=models.ForeignKey(User,on_delete=models.CASCADE)
    post=models.ForeignKey(Post,on_delete=models.CASCADE)
    parent=models.ForeignKey('self',on_delete=models.CASCADE,null=True )
    timestamp= models.DateTimeFIEld(default=Now)

    def __str__(self):
        return self.comment[0:13] + "..." + "by" + " " + self.user.username
  1. HTML 页面

    @H_502_7@           <h2 class="blog-post-Title">{{post.Title}}</h2>
                <p class="blog-post-Meta">{{post.timeStamp}} by <a href="#">{{post.author}}</a></p>
    
    
           <p>{{post.content|safe}}</p>
    
           <hr>
         </div>
         </div>
      </div>
    
         {% if user.is_authenticated %}
        <form action="{% url 'fitness:postComment' %}" method="post">
            {% csrf_token %}
            <div class="form-group">
                <label for="exampleinputEmail1">Post Comment </label>
                <input type="text" class="form-control" name="comment" placeholder="Enter comment here">
            </div>
            <input type="hIDden" name="postsno" value="{{post.sno}}">
            <input type="hIDden" name="parentSno" value="">
            <@R_301_5554@ type="submit" class="btn btn-primary">submit</@R_301_5554@>
        </form>
        {% else %}
        Please login to post a comment
        {% endif %}
    </div>
    {% for comment in comments %}
    <div class="row my-3">
        <div class="col-md-1  ">
            <img class="rounded mx-auto d-block w-100 border border-dark p-2" src="http://i9.photobucket.com/albums/a88/creaticode/avatar_1_zps8e1c80cd.jpg" alt="user">
    
        </div>
        <div class="col-md-11 ">
          <h6 class="comment-name by-author"> {{comment.user.username}} </h6> <span class="badge badge-secondary "></span>
       <b> {{comment.comment}} </b> <span class="badge badge-secondary "></span><br>
       <@R_301_5554@ class="btn btn-sm btn-primary" type="@R_301_5554@" data-toggle="collapse" data-target="#replyBox{{comment.sno}}" aria-expanded="false" aria-controls="replyBox{{comment.sno}}">
           Reply
       </@R_301_5554@>
       <@R_301_5554@ class="btn btn-sm btn-primary" type="@R_301_5554@" onClick="myFunction()" ID= "show-hIDe" >
           Show ReplIEs
       </@R_301_5554@>
    
    
            <div class="reply mx-0" >
    
    
                <div class="collapse" ID="replyBox{{comment.sno}}">
                    <div class="card card-body my-2" >
                        <form action="{% url 'fitness:postComment' %}" method="post">
                            {% csrf_token %}
                            <div class="form-group" >
                                <label for="comment">Post a reply </label>
                                <input type="text" class="form-control" name="comment" placeholder="Enter comment here">
                                <input type="hIDden" name="parentSno" value="{{comment.sno}}">
                            </div>
                            <input type="hIDden" name="postsno" value="{{post.sno}}">
                            <@R_301_5554@ type="submit" class="btn btn-primary">submit</@R_301_5554@>
                        </form>
                    </div>
                </div>
    
                <div class="replIEs bg-danger my-2" ID = "replIEs" >
                {% for reply in replyDict|get_val:comment.sno %}
                <div class="replIEs">{{reply}}</div>
                <br>
                {% endfor %}
                </div>
            </div>
     </div>
    </div>
    {% endfor %}
    
    <!-- Contenedor Principal -->
    
    <script>
       function myFunction(){
         var x = document.getElementByID("replIEs");
         if(x.style.display === "none"){
           x.style.display = "block";
    
         }
         else{
           x.style.display = "none";
         }
       }
    </script>
    

我认为这是一个逻辑错误。有人可以帮我找出这个错误吗?虽然一切正常,但我的回复按钮无法正常工作。

解决方法

models.py 的变化。更新 parent 中的 BlogComment(添加 related_name

parent=models.ForeignKey('self',on_delete=models.CASCADE,null=True,related_name='replies' )

views.py 处发生变化comments

comments= BlogComment.objects.filter(post=post,parent=None).prefetch_related('replies')

您不需要单独查询回复。

html中(仅用于回复。请根据您的要求进行更改)

{% for comment in comments %}
      {% for reply in comment.replies.all %}
        {{ reply.comment }}
      {% endfor %}
{% endfor %}

这会起作用。记住related_name=replies

(抱歉回答不清楚。办公室下班了,我要走了。祝你好运)

大佬总结

以上是大佬教程为你收集整理的显示回复按钮对评论不起作用全部内容,希望文章能够帮你解决显示回复按钮对评论不起作用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: