Django DetailView with form for comments

2019-08-20 09:43发布

问题:

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.

回答1:

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)


回答2:

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.



标签: django forms