OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Why FormView wont save the object while CreateView does?

  • Thread starter Thread starter AArgh
  • Start date Start date
A

AArgh

Guest
I am still new in django, i searched in FormView and CreateView source code and i found they both inheritate form same things

this is my simple view which has inherited from FormView

Code:
class MyFormView(FormView):
    form_class = MyForm
    template_name = 'form.html'
    success_url = "/thanks/"

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

i overwrote the form_valid method to try save my object

but in my CreateView

Code:
class PersonCreateView(CreateView):
    model = Person
    fields = '__all__'
    template_name = 'form.html'

it saves my object automatically
<p>I am still new in django, i searched in FormView and CreateView source code and i found they both inheritate form same things</p>
<p>this is my simple view which has inherited from FormView</p>
<pre><code>class MyFormView(FormView):
form_class = MyForm
template_name = 'form.html'
success_url = "/thanks/"

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

</code></pre>
<p>i overwrote the form_valid method to try save my object</p>
<p>but in my CreateView</p>
<pre><code>class PersonCreateView(CreateView):
model = Person
fields = '__all__'
template_name = 'form.html'

</code></pre>
<p>it saves my object automatically</p>
 

Latest posts

Top