好的,这将是我最后一次尝试,为什么我不能让FormComment在detail.html中评论帖子?我对这件事感到紧张
def DetailView(request, pk):
if request.user.is_authenticated():
base_template_name = 'blog/base.html'
else:
base_template_name = 'blog/visitor.html'
post = get_object_or_404(Post, pk=pk)
form = CommentForm(request.POST or None)
if form.is_valid():
comment = form.save(commit=False)
comment.post = post
comment.save()
return redirect('blog:DetailView')
return render(request,'blog/detail.html',{'post':post,'from':form ,'base_template_name':base_template_name})
form.py
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = ('textc',)
这里是detail.html(它的一部分)
{% if post.comment_set.all %}
<h2>Comments</h2>
<div class="comments">
{% for comment in post.comment_set.all %}
<span>
<a href="">{{ comment.user }}</a> said on {{ comment.created_on }}
</span>
<p>
{{ comment.textc }}
</p>
{% endfor %}
</div>
{% endif %}
<h4>Leave a Comment:</h4>
<form action="" method="POST">
{% csrf_token %}
<table>
{{ form.as_p }}
</table>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
模型:
class Comment(models.Model):
post = models.ForeignKey('blog.Post', null=True,related_name='comments')
user = models.ForeignKey(User, max_length=200, blank=True, null=True)
textc = models.TextField(null=True,max_length=200)
created_date = models.DateTimeField(default=timezone.now)
您在视图的最后一行有一个输入错误;在模板上下文中有
from
而不是form
相关问题 更多 >
编程相关推荐