I have this url mapping:
url(r'^article/(?P<slug>[-\w]+)/$', views.ArticleView.as_view(), name='article-detail'),
and I have this view:
class ArticleView(DetailView):
model = Article
template_name = 'template/article.html'
context_object_name = 'article'
def get_context_data(self, **kwargs):
context = super(ArticleView, self).get_context_data(**kwargs)
context['comments'] = self.object.comment_set.filter(approved=True)
return context
I've already displayed all the approved comments (as you see), but I don't know how to create a comment form inside that ArticleView.
I have this ModelForm:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = '__all__'
and... Comment model:
class Comment(models.Model):
author = models.CharField(max_length=100)
article = models.ForeignKey(Article, on_delete=models.CASCADE)
email = models.EmailField()
message = models.TextField(max_length=1000)
created_at = models.DateTimeField(auto_now_add=True)
approved = models.BooleanField(default=False)
The problem with CommentForm is that I don't know how to 'hide' article and approved fields and how to fill article field with the article got in the ArticleView.
I've tried to combine FormMixin with DetailView but.. when I submit the
comment form, console displays: Method not Allowed (POST).
How can I create a form view into ArticleView?
If you didn't get something, please ask me, I know my grammar is bad. I will try to be clear as much as possible.
Thanks in advance for answers.
I solved it, kind of..
class ArticleView(FormMixin, DetailView):
model = Article
template_name = 'template/article.html'
form_class = CommentForm
def get_success_url(self):
return reverse('article-detail', kwargs={'slug': self.object.slug})
def get_context_data(self, **kwargs):
context = super(ArticleView, self).get_context_data(**kwargs)
context['form'] = CommentForm(initial={
'article': self.object
})
context['comments'] = self.object.comment_set.filter(approved=True)
return context
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
form.save()
return super(ArticleView, self).form_valid(form)
in forms.py:
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
exclude = ('admitted',)
widgets = {
'article': forms.HiddenInput()
}
The only way I could... set a value for that article field (which is a foreign key for the article) was to set initial value in ArticleView.
If someone have a better alternative, I'm glad too see it.
Setting a temporary variable like this and you won't have to set an initial value in ArticleView
Simply as this
context['form'] = self.get_form()
and this
def form_valid(self, form):
new_comment = form.save(commit=False)
new_comment.post = self.get_object()
return super(ArticleView, self).form_valid(form)
I know it's been years, but maybe my answer will be useful for someone Yierstem's answer worked for me, but I made a few changes
First, changed get_success_url in order to use parent model's get_absolute_url:
def get_success_url(self):
return self.object.get_absolute_url()
Add get_absolute_url method to parent model first:
def get_absolute_url(self):
return reverse('record_detail', args=[str(self.pk)]
Second, I add my target article and author (ForeignKey(s) of my Comment model) inside form_valid method:
def form_valid(self, form):
new_comment = form.save(commit=False)
new_comment.target = self.object
new_comment.author = self.request.user
new_comment.save()
return super(RecordDetailView, self).form_valid(form)