【问题标题】:Django-Blog comments form not appearingDjango-Blog 评论表单没有出现
【发布时间】:2020-02-21 16:15:27
【问题描述】:

我目前正在关注 Django-Central 站点上的 Django-Blog 教程,并且正在尝试将评论部分添加到博客中。我已经显示了我的 cmets,但我似乎无法显示表单,以便网站访问者可以将 cmets 添加到博客文章中。

views.py

from django.shortcuts import render, get_object_or_404
from django.views import generic
from .models import Post
from .forms import CommentForm


# Create your views here.
class PostList(generic.ListView):
    queryset = Post.objects.filter(status=1).order_by('-created_on')
    template_name = 'index.html'


class PostDetail(generic.DetailView):
    model = Post
    template_name = 'post_detail.html'


def post_detail(request, slug):
    template_name = 'post_detail.html'
    post = get_object_or_404(Post, slug=slug)
    # Fetching the comments that have active=True
    comments = post.comments.filter(active=True)
    new_comment = None

    # Comment posted
    if request.method == 'POST':
        comment_form = CommentForm(data=request.POST)
        if comment_form.is_valid():
            # Create Comment object but don't save to database yet
            new_comment = comment_form.save(commit=False)
            # Assign the current post to the comment
            new_comment.post = post
            # Save the comment to the database
            new_comment.save()
    else:
        comment_form = CommentForm()

        print('Comments:', comments)
        return render(request,
                      template_name,
                        {'post': post,
                        'comments': new_comment,
                        'new_comment': new_comment,
                        'comment_form': comment_form})


forms.py

from .models import Comment
from django import forms


class CommentForm(forms.ModelForm):
    class Meta:
        model = Comment
        fields = ('name', 'email', 'body')

post_detail.html

<div class="col-md-8 card mb-4  mt-3">

                <div class="card-body">
                    <!-- comments -->
                    <h2>{{ post.comments.count }} comments</h2>

                    {% for comment in post.comments.all %}
                    <div class="comments" style="padding: 10px;">
                        <p class="font-weight-bold">
                            {{ comment.name }}
                            <span class="">
                                {{ comment.created_on }}
                           </span>
                        </p>
                        {{ comment.body | linebreaks }}
                    </div>
                    {% endfor %}
                </div>

                <div class="card-body">
                    {% if new_comment %}
                    <div class="alert alert-success" role="alert">
                        Your comment is awaiting moderation
                    </div>
                    {% else %}
                    <h3>Leave a comment</h3>
                    <form method="post" style="margin-top: 1.3em;">
                        {% csrf_token %}
                        {{ comment_form.as_p }}
                        <button type="submit" class="btn btn-primary btn-lg">Submit</button>
                    </form>
                    {% endif %}
                </div>
            </div>

非常感谢任何意见。

【问题讨论】:

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


    【解决方案1】:

    您引用了错误的模板上下文,表单不存在,您应该使用comment_form,这就是您在render 方法中的调用方式。

    应该是{{ comment_form.as_p }}

    【讨论】:

    • 我也尝试过这样做,但它仍然不显示。这就是为什么我现在完全迷路了。
    • 我也看到你的代码有错误comment_form = CommentForm(data=request.PSOT),应该是POST
    • 另外,我没有看到这个页面在第一次查看时是如何呈现的,处理这个页面的 GET 请求的代码在哪里?
    • 哦该死的没看到。好吧,我改变了它,但不幸的是仍然没有运气,但我确信这解决了未来的问题。谢谢你
    • 哪些 GET 请求?我不明白,我很新,所以我按照 Django-Central 站点上的 Django-Blog 教程进行操作。但这似乎已经过时或其他什么,因为它似乎不再起作用了。即使试图复制粘贴来替换我的代码并跳转回来查看差异,他们的代码也不起作用所以我不确定是怎么回事
    猜你喜欢
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多