Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery Django Ajax注释正在更新和显示,但内容未从注释表单中删除_Jquery_Python_Html_Ajax_Django - Fatal编程技术网

Jquery Django Ajax注释正在更新和显示,但内容未从注释表单中删除

Jquery Django Ajax注释正在更新和显示,但内容未从注释表单中删除,jquery,python,html,ajax,django,Jquery,Python,Html,Ajax,Django,当我过去评论时,它在div上显示得非常好,但是没有从评论表单中删除该值,再次评论时,我必须刷新页面才能这样做 请参考查看功能和主要JS部分 请在这方面给予帮助,谢谢 views.py def post_detail(request, slug): post = get_object_or_404(Post, slug=slug) comments = Comment.objects.filter(post=post, parent=None) if request.metho

当我过去评论时,它在div上显示得非常好,但是没有从评论表单中删除该值,再次评论时,我必须刷新页面才能这样做

请参考查看功能和主要JS部分

请在这方面给予帮助,谢谢

views.py

def post_detail(request, slug):
    post = get_object_or_404(Post, slug=slug)
    comments = Comment.objects.filter(post=post, parent=None)

if request.method == 'POST':
    if not request.user.is_authenticated():
        raise Http404
    comment_form = CommentForm(request.POST or None)
    if comment_form.is_valid():
        content = comment_form.cleaned_data['content']
        parent_id = request.POST.get("parent_id")
        print("Parent ID: ", parent_id)
        parent_qs = None
        if parent_id:
            parent_qs = Comment.objects.filter(id=parent_id).first()
            print("Parent QS: ", parent_qs)
        new_comment, created = Comment.objects.get_or_create(
            user=request.user,
            post=post,
            content=content,
            parent=parent_qs
        )
else:
    comment_form = CommentForm()

context = {
    "post": post,
    "comments": comments,
    "comment_form": comment_form,
}
if request.is_ajax():
    return render(request, 'posts/partial_post_detail.html', context)
else:
    return render(request, 'posts/post_detail.html', context)
post_detail.html

{% extends 'base.html' %} 
{% block title %}{{ post.title }}{% endblock %} 
{% block content %} 

{{ post.title }}<br> 
Published by {{ post.user }}<br><br> 
{{ post.content|linebreaks }}<br> 
{% if post.user == request.user %} 
    <a href='{% url "posts:post_edit" slug=post.slug %}'>Edit</a>
    <a href='{% url "posts:post_delete" slug=post.slug %}'>Delete</a> 
{% endif %}
<hr>

<h1><b>Comments</b></h1>

<div id="div1">
    {% include 'posts/partial_post_detail.html' %}
</div>
{% endblock %}
Models.py

class Comment(models.Model):
user = models.ForeignKey(User, default=1)
post = models.ForeignKey(Post)
parent = models.ForeignKey('self', null=True, blank=True, related_name='replies')
# content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
# object_id = models.PositiveIntegerField()
# content_object = GenericForeignKey('content_type', 'object_id')

content = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True)

class Meta:
    ordering = ['-timestamp']

def __str__(self):
    return "{} - {}".format(str(self.user.username), str(self.post.title))
主要内容:

 <script>
    /* global $ */
    $(document).ready(function(event) {
        $("textarea").val("");
        $('.reply-btn').click(function() {
            console.log("Reply Button");
            $(this).parent().next(".replied-comments").fadeToggle();

        });

        $("#comment-form").submit(function(event) {
            event.preventDefault();
            $.ajax({
                data: $(this).serialize(),
                type: $(this).attr('method'),
                url: $(this).attr('action'),
                success: function(data){
                    console.log(data['form']);
                    data = data['form']
                    $("#div1").html(data);
                    $("textarea").val("");
                    $('.reply-btn').click(function() {
                        $(this).parent().next(".replied-comments").fadeToggle();
                    });

                },
                error: function(e){
                    console.log(e);
                }
            });
        });


        $("#reply-form").submit(function(event) {
            event.preventDefault();
            $.ajax({
                data: $(this).serialize(),
                type: $(this).attr('method'),
                url: $(this).attr('action'),
                success: function(data){
                    data = data['form'];
                    console.log(data);
                    $("#div1").html(data);
                    $("textarea").val("");
                    $('.reply-btn').click(function() {

                        $(this).parent().next(".replied-comments").fadeToggle();
                        $('textarea').val('');
                    });
                },
                error: function(e){
                    alert(e);
                }
            });
        });

        // $("#id_username").prop("disabled", true);
        // $("#id_email").prop("disabled", true);

    });

</script>

/*全球美元*/
$(文档).ready(函数(事件){
$(“textarea”).val(“”);
$('.reply btn')。单击(函数(){
控制台日志(“回复按钮”);
$(this.parent().next(“.responsed comments”).fadeToggle();
});
$(“#评论表”)。提交(功能(事件){
event.preventDefault();
$.ajax({
数据:$(this).serialize(),
类型:$(this.attr('method'),
url:$(this.attr('action'),
成功:功能(数据){
日志(数据['form']);
数据=数据['form']
$(“#div1”).html(数据);
$(“textarea”).val(“”);
$('.reply btn')。单击(函数(){
$(this.parent().next(“.responsed comments”).fadeToggle();
});
},
错误:函数(e){
控制台日志(e);
}
});
});
$(“#答复表”)。提交(功能(事件){
event.preventDefault();
$.ajax({
数据:$(this).serialize(),
类型:$(this.attr('method'),
url:$(this.attr('action'),
成功:功能(数据){
数据=数据[‘形式’];
控制台日志(数据);
$(“#div1”).html(数据);
$(“textarea”).val(“”);
$('.reply btn')。单击(函数(){
$(this.parent().next(“.responsed comments”).fadeToggle();
$('textarea').val(“”);
});
},
错误:函数(e){
警报(e);
}
});
});
//$(“#id_username”).prop(“disabled”,true);
//$(“#id_email”).prop(“已禁用”,true);
});
使用此

$("#comment-form").submit(function(event) {
    event.stopPropagation();
    var myform = $("#comment-form");
        var data= $(this).serialize();
        alert(data);
        $.ajax({
        data: $(myform).serialize(),
                type: $(myform).attr('method'),
                url: $(myform).attr('action'),
                success: function(data){
                    console.log(data);
                    $("#div1").html(data);
                    $(myform +" textarea").val(' ');
                    $(myform +" input").val(' ');
                },
                error: function(e){
                    console.log(e);
                }
    });
});

我在$(“#div1”).html(数据)之前使用了$(“textarea”).val(“”);但在再次评论并提交表单后,它会重新加载整个页面,我不希望出现这种情况。请看一下代码。嗨,Mahindra,谢谢你现在回复。当我第一次回复评论时,问题出现了。当回复同一条评论时,页面得到了刷新。当回复同一条评论时,它只是使用了我想要的ajax,但如果我再次回复同一条评论,页面将被刷新。请在这方面提供帮助。单击回复时,您可以告诉我您正在调用哪个函数,或者只需在此处发送用于回复注释的代码到具有父id的同一post_详细视图。您必须使用相同的JavaScript函数,只需将“$(“#comment form”)”替换为“$(“#reply form”)“并且必须根据需要保存数据。
 <script>
    /* global $ */
    $(document).ready(function(event) {
        $("textarea").val("");
        $('.reply-btn').click(function() {
            console.log("Reply Button");
            $(this).parent().next(".replied-comments").fadeToggle();

        });

        $("#comment-form").submit(function(event) {
            event.preventDefault();
            $.ajax({
                data: $(this).serialize(),
                type: $(this).attr('method'),
                url: $(this).attr('action'),
                success: function(data){
                    console.log(data['form']);
                    data = data['form']
                    $("#div1").html(data);
                    $("textarea").val("");
                    $('.reply-btn').click(function() {
                        $(this).parent().next(".replied-comments").fadeToggle();
                    });

                },
                error: function(e){
                    console.log(e);
                }
            });
        });


        $("#reply-form").submit(function(event) {
            event.preventDefault();
            $.ajax({
                data: $(this).serialize(),
                type: $(this).attr('method'),
                url: $(this).attr('action'),
                success: function(data){
                    data = data['form'];
                    console.log(data);
                    $("#div1").html(data);
                    $("textarea").val("");
                    $('.reply-btn').click(function() {

                        $(this).parent().next(".replied-comments").fadeToggle();
                        $('textarea').val('');
                    });
                },
                error: function(e){
                    alert(e);
                }
            });
        });

        // $("#id_username").prop("disabled", true);
        // $("#id_email").prop("disabled", true);

    });

</script>
$("#comment-form").submit(function(event) {
    event.stopPropagation();
    var myform = $("#comment-form");
        var data= $(this).serialize();
        alert(data);
        $.ajax({
        data: $(myform).serialize(),
                type: $(myform).attr('method'),
                url: $(myform).attr('action'),
                success: function(data){
                    console.log(data);
                    $("#div1").html(data);
                    $(myform +" textarea").val(' ');
                    $(myform +" input").val(' ');
                },
                error: function(e){
                    console.log(e);
                }
    });
});