Python 没完没了的评论会带来麻烦
现在,我能够呈现一组带有回复的Python 没完没了的评论会带来麻烦,python,ajax,django,django-models,django-views,Python,Ajax,Django,Django Models,Django Views,现在,我能够呈现一组带有回复的注释(也是注释)查询集。然而,我不确定如何进一步,并允许回复回复,等等。这是我的密码: class Comment(models.Model): user = models.ForeignKey(User, blank=True, null=True) destination = models.CharField(default='1', max_length=12, blank=True) parent_id = models.Intege
注释
(也是注释
)查询集。然而,我不确定如何进一步,并允许回复回复,等等。这是我的密码:
class Comment(models.Model):
user = models.ForeignKey(User, blank=True, null=True)
destination = models.CharField(default='1', max_length=12, blank=True)
parent_id = models.IntegerField(default=0)
parent_comment = models.ForeignKey('self', related_name='replies', related_query_name='replies', blank=True, null=True)
comment_text = models.TextField(max_length=350, blank=True, null=True)
timestamp = models.DateTimeField(default=timezone.now, blank=True)
children = models.IntegerField(default=0)
def __str__(self):
return str(self.comment_text)
我的家长评论视图:
def user_comment(request):
if request.is_ajax():
comment = CommentForm(request.POST or None)
ajax_comment = request.POST.get('text')
id = request.POST.get('id')
comment_length = len(str(ajax_comment))
if comment.is_valid() and request.user.is_authenticated:
comment = Comment.objects.create(comment_text=ajax_comment, destination=id, user=request.user)
username = str(request.user)
return JsonResponse({'text': ajax_comment, 'text_length': comment_length,
'username': username, 'id': comment.id})
else:
return HttpResponse()
我的答复是:
def comment_reply(request):
if request.is_ajax():
comment = CommentForm(request.POST or None)
reply_text = request.POST.get('reply_text')
id = request.POST.get('id')
parent_id = request.POST.get('parent_id')
parent = Comment.objects.get(id=parent_id)
parent.children += 1
parent.save()
if comment.is_valid() and request.user.is_authenticated:
comment = Comment.objects.create(comment_text=reply_text, destination=id, user=request.user, parent_id=parent_id, parent_comment=parent)
username = str(request.user)
return JsonResponse({'reply_text': reply_text, 'username': username})
else:
return HttpResponse()
ajax调用
var str = window.location.href.split('?')[0];
var path = str.split("/")[4];
$('.comment_form').on('submit', function(e) {
e.preventDefault();
var c = $(this).find('.comment_text').val()
console.log('this:', c);
$.ajax({
type: 'POST',
url: '/user_comment/',
data: {
text: $(this).find('.comment_text').val(),
id: path,
csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val(),
},
success: function(data) {
if(data.text == '') {
console.log('Cannot submit blank comment');
} else {
//console.log('')
$('.commentsContainer hr').prepend("<div class='comment_div'><div class='left_comment_div'>" +
"<div class='username_and_votes'><h3><a class='username_foreign'>" + data.username +
"</a></h3></div><br><p>" + data.text +
"</p></div></div>");
}}
});
});
// reply comment
$(document).on('submit', '.reply_comment_form', function(e) {
e.preventDefault();
parent_id = $('.reply_comment_form').data('comment_id');
$.ajax({
type: 'POST',
url: '/comment_reply/',
data: {
reply_text: $(this).find('.comment_text').val(),
parent_id: parent_id,
id: path,
csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val(),
},
success: function(data) {
$('.reply_comment_form').replaceWith("<div class='comment_div new_comment' style='display: inline-block;'><div class='left_comment_div'>" +
"<div class='username_and_votes'><h3><a href='#' class='username_foreign'>" + data.username +
"</a></h3><br><p>" + data.reply_text +
"</p></div></div>");
}
});
});
var str=window.location.href.split('?')[0];
var path=str.split(“/”[4];
$('.comment_form')。关于('submit',函数(e){
e、 预防默认值();
var c=$(this.find('.comment_text').val()
log('this:',c);
$.ajax({
键入:“POST”,
url:“/user_comment/”,
数据:{
text:$(this.find('.comment_text').val(),
id:路径,
csrfmiddlewaretoken:$(“输入[name='csrfmiddlewaretoken']”)。val(),
},
成功:功能(数据){
如果(data.text==''){
console.log('无法提交空白注释');
}否则{
//console.log(“”)
$('.commentsContainer hr')。前缀(“'+
“
”+data.reply\u文本+
“”;
}
});
});
和评论html
<div class="commentsContainer">
<form action="" class="comment_form">{% csrf_token %}
{{ comment.comment_text|add_class:"comment_text" }}
{{ comment.id }}
<input type="submit" value="Comment" class="comment_submit">
</form>
<hr>
{% for i in comment_list %}
<div class='comment_div' data-comment_id="{{ i.id }}">
<div class="left_comment_div">
<div class="username_and_votes">
<h3><a class='username_foreign'>{{ i.user }}</a></h3>
</div>
<br>
<p>{{ i.comment_text }}</p>
</div>
</div>
{% for reply in i.replies.all %}
<div class='comment_div new_comment' data-comment_id="{{ reply.id }}">
<div class="left_comment_div">
<div class="username_and_votes">
<h3><a class='username_foreign'>{{ reply.user }}</a></h3>
</div>
<br>
<p>{{ reply.comment_text }}</p>
</div>
</div>
{% endfor %}
{% endfor %}
</div>
{%csrf_令牌%}
{{comment.comment_text}添加_类:“comment_text”}
{{comment.id}
{注释列表%中的i的%
{{i.user}}
{{i.comment_text}
{i.replies.all%中答复的百分比}
{{reply.user}
{{reply.comment_text}
{%endfor%}
{%endfor%}
如果有人能给我一些建议,告诉我如何从我目前拥有的代码中实现无休止的回复,那就太好了。问题出在哪里?无限递归?当我回复回复回复时,它不会呈现。注释成功保存在数据库中,但不会显示。是的,我想是无限递归。