Reputation: 56
Ok this will be my last try, why I can't get the FormComment to comment a post in detail.html ? I'm getting nervous about this..
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',)
And here the detail.html ( part of it)
{% 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>
the CommentModel:
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)
Upvotes: 0
Views: 45
Reputation: 599530
You have a typo in the last line of the view; you have from
instead of form
in the template context.
Upvotes: 2