I'm trying to create a blog with comments. I've extended the generic DetailView so that I can save and display comments, but, although it seems to pass the information I want, nothing is displayed in the template.
Here is the code for the extended generic view.
class BlogDetailView(DetailView):
context ={}
form = CommentForm()
context['form'] = form
context_object_name = "comments"
model = Comments
def get_context_data(self, **kwargs):
form = CommentForm()
if self.request.method =='POST':
if form.is_valid():
t = form.save(commit=False)
t.time = datetime.datetime.now()
t.save()
return HttpResponseRedirect(reverse('blog/blogpost_detail.html'))
context = super(BlogDetailView, self).get_context_data(**kwargs)
context['form'] = form
context['comments'] = Comments.objects.all()
return render(self.request, 'blog/blogpost_detail.html', context)
here is the code from the form I'm trying to pass
class CommentForm(forms.ModelForm):
class Meta:
model = Comments
fields=('commentText',)
exclude =('post','commentTime','commentImage',)
widgets={
'commentText': forms.Textarea(attrs={'cols': 80, 'rows': 20}),
}
and here is the relevant code from the template that I'm trying to display the form on.
<!-- Post Content -->
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<table>
{% for comment in comments %}
<p>{{ comment }}</p>
{% endfor %}
<div class="test">
<form role="form" method="post">
<div class="input-group">
{% csrf_token %}
{{ form }}
{% for field in form %}
{{ field }}
{% endfor %}
<p>Comment: </p>
<span class="input-group-btn">
<button class="btn btn-default" type="submit">Submit</button>
</span>
</div>
</form>
</div>
</table>
</form>
</div>
</div>
</div>
</article>
As of now, the page will load but the text field will not display. When I click the submit button it loads a blank page with the same url as before. I also think I might be missing some things as I am very new to Django.
Thanks for the help!
Aucun commentaire:
Enregistrer un commentaire