1

I am getting forms success message in my html template but forms data not saving. here is my code:

views.py:

 class BlogDetailsAccount(FormMixin,DetailView):
          model = Blog
          template_name = 'blog/my-account-blog-details.html'
          form_class = CommentFrom
         
          def get_success_url(self):
            return reverse('blog:my-account-blog-details', kwargs={'slug': self.object.slug})
      
          def get_context_data(self, **kwargs):
              #my context data........
              
              return data
          
          def post(self, request, *args, **kwargs):
            self.object = self.get_object()
            form = self.get_form()
            if form.is_valid():
                messages.add_message(self.request, messages.INFO, 'Your Comment pending for admin approval')
                return self.form_valid(form)
            else:
                messages.add_message(self.request, messages.INFO, 'Somethings Wrong. Please try again')
                return self.form_invalid(form)
    
          def form_valid(self, form):
            return super(BlogDetailsAccount, self).form_valid(form) 

my models.py:

class BlogComment(models.model):
     .......#my models fields.... 
     .............

post_save.connect(BlogComment.user_comment, sender=BlogComment)  #using signals 
8
  • A formMixin does not save the data to the database. Commented Jul 7, 2021 at 15:51
  • Willem Van Onsem what I need to be do for save data? Commented Jul 7, 2021 at 15:52
  • Why don't you simply use an UpdateView? you don't even need to inherit from DetailView in that case since it will pass the object to the view. Commented Jul 7, 2021 at 15:55
  • @AbdulAzizBarkat: I think the form is to create a comment, whereas the view displays a blog, so the form does not match the object of the DetailView. Commented Jul 7, 2021 at 15:56
  • @boyenec please make sure your edits don't invalidate answers. See “What to do when someone answers” - Don't be a chameleon, don't be a vandal. You may edit, but edit in such a way that it doesn't invalidate existing answers. Commented Jul 7, 2021 at 16:24

1 Answer 1

2

A FormMixin does not save the data to the database. In case you need to save the form, you should save it in the form_valid method:

A form_mixin does not save the data to the database.

class BlogDetailsAccount(FormMixin,DetailView):
    # …

    def post(self, request, *args, **kwargs):
        form = self.get_form()  # ← first create a form
        self.object = self.get_object()  # ← then specify the object
        if form.is_valid():
            messages.add_message(self.request, messages.INFO, 'Your Comment pending for admin approval')
            return self.form_valid(form)
        else:
            messages.add_message(self.request, messages.INFO, 'Somethings Wrong. Please try again')
            return self.form_invalid(form)

    def form_valid(self, form):
        form.save()
        return super().form_valid(form)

You should also likely fill in relevant data like the post where the comment appears and

Sign up to request clarification or add additional context in comments.

9 Comments

I tried form.save() before posting the question but didn't work. I am getting this error 'NoneType' object has no attribute 'author' I think I need to pass author id inside view but I am not understanding how
@boyenec: edit the question with the full traceback, share the form you are using.
@boyenec: you can work with form.instance.author = self.request.user before saving the form.
@boyenec it is likely from your signal, you seem to define it on your model which is a bad practice, define it in a separate file.
@Willem Van Onsem my problem solved. I was doing mistake here form.instance.author = self.request.user it will be form.instance.user = self.request.user as I was using user fields and author was for another model.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.