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.
Setting a temporary variable like this and you won't have to set an initial value in ArticleView
Simply as this
and this
I solved it, kind of..
in
forms.py
: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.