评论回复在django(评论)

时间:2018-04-02 13:33:36

标签: python django django-models django-templates django-views

我在django中写了自己的评论系统,但我不知道它是否是实现这一功能的正确方法。 评论样式就像是facebook,因此您有一条评论和与该评论相关的回复。 请查看我的代码。

models.py

class Comment(models.Model):
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE)
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    message = models.CharField(max_length = 250)


class CommentReply(models.Model):
    whichcomment = models.ForeignKey(Comment, on_delete=models.CASCADE)
    user = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    message = models.CharField(max_length = 250)
    blog = models.ForeignKey(Blog, on_delete=models.CASCADE)

views.py

def PostDetail(request, pk):
    form1 = CommentForm
    form2 = CommentReplyForm
    data1 = Blog.objects.get(pk=pk)
    com = Comment.objects.filter(blog=data1)
    replycom = CommentReply.objects.filter(blog=data1)

如果request.method ==' POST':

if request.POST.get('whichcomment'):
    data3 = CommentReplyForm(request.POST)

    if data3.is_valid():
        whichcomment = int(request.POST.get('whichcomment'))
        message = data3.cleaned_data['message']

        CommentReply.objects.create(
            whichcomment = Comment.objects.get(id=whichcomment),
            user = User.objects.get(username=request.user.username),
            message = message,
            blog=data1

        ).save()

        return HttpResponseRedirect(reverse('postdetail', args=(), kwargs={'pk': pk}))
        #return render(request, 'postdetail.html', {'data1': data1, 'form1':form1, 'com':com, 'form2':form2, 'replycom':replycom})


else:
    data2 = CommentForm(request.POST)

    if data2.is_valid():
        blog = data1,
        message = data2.cleaned_data['message']


        Comment.objects.create(
            blog=data1,
            user= User.objects.get(username=request.user.username),
            message=message
        ).save()

        return HttpResponseRedirect(reverse('postdetail', args=(), kwargs={'pk': pk}))

否则:     return render(request," postdetail.html',{' data1&#39 ;: data1,' form1':form1,' com':com, ' form2':form2,' replycom':replycom})

模板文件

<form method="POST">
{% csrf_token %}

{{ form1 }}

<button type="submit">submit</button>
</form>

{% for comments in com %}
<p>{{ comments.user }} --- {{ comments.message }}
{% if comments.user == request.user %}<a href="{% url 'commentdelete' comments.pk %}">delete</a>{% endif %}

<br>
<form method='POST'>
    {% csrf_token %}

    <label for="id_whichcomment">whichcomment</label>
    <input type="hidden" name='whichcomment' id='id_whichcomment' value="{{comments.id}}">
    &nbsp;
    <label for="id_message">Message:</label>
    <input type="text" name='message' maxlength="250" id="id_message">

    <button type="submit">submit</button>
</form>

{% for r in replycom %}

{% if r.whichcomment.message == comments.message %}

<p><blockquote>{{r.user }} -- {{ r.message }}
{% if r.user == request.user %}<a href="{% url 'commentreplydelete' r.pk %}">delete</a>{% endif %}</blockquote></p>

{% endif %}

{% endfor %}
</p>

<br>
{% endfor %}

0 个答案:

没有答案