Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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
Django-评论表_Django_Django Forms_Django Views - Fatal编程技术网

Django-评论表

Django-评论表,django,django-forms,django-views,Django,Django Forms,Django Views,我有4个模型:用户、博客、帖子和评论 现在,在“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

我有4个模型:用户、博客、帖子和评论

现在,在“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 %}
后模型:

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()
我是在我看来这么做的

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'”。

您必须创建一个带有pk参数的url,才能知道在哪个帖子中发布评论:

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


您必须创建一个带有pk参数的url,才能知道在哪篇文章中发表评论:

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


您的代码唯一的错误是没有
request.topic
。主题是你已经拥有的帖子

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

您的代码唯一的错误是没有
request.topic
。主题是你已经拥有的帖子

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

我在我的博客项目中也有类似的东西,我用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' %}
注释;

{%if post.comments.count我在我的博客项目中有类似的东西,我是为了用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' %}
注释;


{%if post.comments.count只有在您更新问题以包含注释模型时,我才能提供帮助:)我们不需要模型,而需要视图。@mohammedqudah更新。@DanielRoseman视图也会更新。您还需要给出实际错误。该代码中没有对
request.post
的引用,因此不会给出该错误;它可能会给你一个
请求的错误。但是主题
。只有当你更新了问题以包含注释模型时,我才能提供帮助:)我们不需要模型,而需要视图。@mohammedqudah updated.@DanielRoseman视图也会更新。你还需要给出实际的错误。在代码中没有对
请求.post
的引用,因此它不会更新i don’我不会给你那个错误;它可能会给你一个错误,比如请求。主题。原始的评论模型没有问题。为什么要更改?我在你更新之前发布了这个答案,所以很抱歉,如果它有帮助,请不要告诉我原始的评论模型没有问题。为什么要更改?我发布了这个答案更新之前,非常抱歉,如果有帮助,请不要向下告诉我。但是在您的视图中,您没有存储诸如评论时间和用户之类的信息。有什么原因吗?为什么不查看我的answer@DeepakRawat正如我之前所说,我只是为了教育目的尝试处理不同的技术。我将这些存储在html页面上。但在您的视图中,您并没有存储在中评论时间和用户。有什么原因吗?为什么不看我的answer@DeepakRawat正如我之前所说,我只是为了教育目的尝试处理不同的技术。我将这些存储在html页面上。