Python Django博客评论表单未出现
我目前正在关注Django中心网站上的Django博客教程,并试图将评论部分添加到博客中。我已经显示了我的评论,但我似乎无法显示表单,以便网站访问者可以向博客帖子添加评论 views.pyPython Django博客评论表单未出现,python,django,django-forms,django-views,django-templates,Python,Django,Django Forms,Django Views,Django Templates,我目前正在关注Django中心网站上的Django博客教程,并试图将评论部分添加到博客中。我已经显示了我的评论,但我似乎无法显示表单,以便网站访问者可以向博客帖子添加评论 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
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>
{{post.comments.count}}comments
{%用于post.comments.all%中的注释}
{{comment.name}
{{comment.created_on}}
{{comment.body | linebreaks}}
{%endfor%}
{%if new_comment%}
你的评论有待审核
{%else%}
留言
{%csrf_令牌%}
{{comment_form.as_p}}
提交
{%endif%}
非常感谢您的任何输入。您引用了错误的模板上下文,表单不存在,您应该使用
注释\u表单
,这就是您在呈现
方法中所称的表单
应该是
{{comment\u form.as\u p}}
我也尝试过这样做,但仍然没有显示出来。这就是为什么我现在完全迷路了。我还看到你的代码comment\u form=CommentForm(data=request.PSOT)
,应该是POST
。而且,我不知道这个页面在第一次查看时是如何呈现的,处理这个页面的GET请求的代码在哪里?哦,该死的,我没看到。嗯,我改变了它,但不幸的是仍然没有运气,但我确信它解决了一个未来的问题。谢谢你的帮助,你收到了哪些请求?我不明白,我是新来的,所以我在Django中心网站上学习了Django博客教程。但这似乎已经过时了,因为它似乎已经不起作用了。即使试图复制粘贴来替换我的代码,然后跳回查看差异,他们的代码也不起作用,所以我不确定出了什么问题