Django - 评论表

时间:2017-10-23 08:17:21

标签: django django-forms django-views

我有4个模型:用户,Blogger,帖子和评论。

现在,在'post_desc.html'中,我想插入一个评论框。

{% if user.is_authenticated %}
  <form method="post">
    {% csrf_token %}
    <input type="text" name="comment" style="width: 800px; height: 145px;"></br></br>
    <button type="submit">Submit Comment</button>
  </form>
{% else %}
  <p><a href="{% url 'login' %}">Login</a> to comment</p>
{% endif %}

因此,此表单仅接受用户的评论。

但是如何存储像'commented_by'这样的信息,这些信息将是当前登录的用户,'commented_on'将是他/她正在评论的post_topic。

如何自动存储这些信息?在views.py我试过'request.user'但是没有用。任何解决方案?

评论模型:

class Comment(models.Model):
    commented_by = models.ForeignKey(User, related_name='comments')
    commented_on = models.ForeignKey(Post, related_name='comments')
    commented_text = models.CharField(max_length=500)
    commented_time = models.DateTimeField(auto_now_add=True)

发布模型:

class Post(models.Model):
    topic = models.CharField(max_length=200)
    description = models.TextField()
    created_by = models.ForeignKey(User, related_name='posts')
    created_on = models.DateTimeField()

我在view.py

中这样做了
def post_desc(request, pk):
    post = get_object_or_404(Post, pk=pk)

    if request.method == 'POST':
        comment = request.POST['comment']
        comments = Comment.objects.create(
            commented_text = comment,
            commented_on = request.topic,
            commented_by = request.user
            )
        return redirect('post_desc', pk=post.pk)

    return render(request, 'post_desc.html', {'post': post})

但是它给出了错误,“'WSGIRequest'对象没有属性'post'”。

3 个答案:

答案 0 :(得分:0)

你必须创建一个具有pk参数的网址,以便知道在哪个帖子中发布评论的位置?

url(r'add/comment/post/(?P<pk>[0-9]+)', views.new_comment, name="comment")

现在让我们创建视图new_comment

from django.utils import timezone
def new_comment(request, pk):
    user = request.user
    comment_text = request.POST['your-comment-input-name']
    post = Post.objects.get(id=pk)
    Comment.objects.create(created_by=user, text=comment_text, post=post, create_at=timezone.localtime(timezone.now()))
    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
  

不要忘记将表单操作设置为action="{% url 'comment' Post.id %}"

答案 1 :(得分:0)

您的代码唯一的问题是没有request.topic这样的东西。主题是你已经拥有的帖子。

    comments = Comment.objects.create(
        commented_text = comment,
        commented_on = post,
        commented_by = request.user
        )

答案 2 :(得分:0)

我在我的博客项目中有类似的东西,我为了在django python中改进自己而开发。

我的模特;

class Comment(models.Model):
post = models.ForeignKey('post.Post', related_name='comments', on_delete=models.CASCADE)
name = models.CharField(max_length=150, verbose_name='Your Name')
comment = models.TextField(verbose_name='Your Comment')
created_date = models.DateTimeField(auto_now_add=True)

我对帖子详情页面的看法;

def post_detail(request, slug):
post = get_object_or_404(Post, slug=slug)

form = CommentForm(request.POST or None)
if form.is_valid():
    comment = form.save(commit=False)
    comment.post = post
    comment.save()
    return HttpResponseRedirect('your redirect page')

context = {
    'post': post,
    'form': form,

}
return render(request, 'post/detail.html', context)

我的帖子详情模板页面;

<h3>Comments;</h3>
        {% if post.comments.count <= 0 %}
            <h4>No comment yet!</h4>
        {% else %}
            {% for comment in post.comments.all %}
                <h4>{{ comment.name }} |
                    <small>{{ comment.created_date|timesince }}</small>
                </h4>
                <p>{{ comment.comment|linebreaks }}</p>
                <hr/>
            {% endfor %}
        {% endif %}

        <h3>Add Comment:</h3>
        {% include 'post/comment.html' %}

我的评论表单模板;

{% load crispy_forms_tags %}

<form method="post">
    {% csrf_token %}
    {{ form|crispy }}
    <input class="btn btn-primary" type="submit" value="Add Comment"/>
</form>

希望这有帮助。