Django not saving form in DB from frontend

71 views
Skip to first unread message

Ali khan

unread,
Sep 29, 2016, 2:54:49 PM9/29/16
to django...@googlegroups.com
Hi,

I am newbie so I must be doing some stupid mistake here.

I am trying to save a contact form with ModelForm. But its not saving in DB from the frontend form.

model.py

class Contact(models.Model):
     name = models.CharField(max_length=100, null=True, blank=True)
     email = models.EmailField()
     send_quote = models.FileField(upload_to='/contacts')

      def __unicode__(self)
           return self.name

forms.py

from .models import Contact
from djagno import forms
from django.forms import ModelForm

class ContactForm(forms.ModelForm):
        class Meta:
                    model = Contact
                    fields = ['name', 'email', 'send_quote']

views.py:

from Django.shortcuts import render, redirect
from .forms import ContactForm

def contact_form(request):
        template = 'contact.html'
        if request.method == 'POST':
               form = ContactForm(request.POST or None, request.Files or None)
               if form.is_valid():
                     new_form = form
                     new_form.save(commit=True)
                     

                return redirect('/')
           else:
                  form = RFPForm
            return render(request, template, context={"form":form})


contact.html:

<form action=" " method="POST">
{% csrf_token %}
{{ form }}
<input type='submit' class='btn btn-danger' value='Send' />
</form>

Please advise.

Ali khan

unread,
Sep 29, 2016, 3:06:43 PM9/29/16
to django...@googlegroups.com
Appologies. In my view after else clause its ContactForm.

ludovic coues

unread,
Sep 29, 2016, 5:19:14 PM9/29/16
to django...@googlegroups.com
I would try to replace

if form.is_valid():
new_form = form
new_form.save(commit=True)

with :

if form.is_valid():
print("Form is valid")
form.save(commit=True)
else:
print("Invalid form")


Pretty sure you will get an invalid form, due to a missing field
send_quote field. For the form to send the file, you need to add an
enctype=multipart or something related.
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users...@googlegroups.com.
> To post to this group, send email to django...@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAAXvsYkTe_LU03ErF%3Dz%3DbNfM3LapOsyQywzKxUP3tm5pxHh28Q%40mail.gmail.com.
>
> For more options, visit https://groups.google.com/d/optout.



--

Cordialement, Coues Ludovic
+336 148 743 42

M Hashmi

unread,
Sep 30, 2016, 11:43:14 PM9/30/16
to django...@googlegroups.com
Please change

def contact_form(request):
        template = 'contact.html'
        if request.method == 'POST':
               form = ContactForm(request.POST or None, request.Files or None)
               if form.is_valid():
                     new_form = form
                     new_form.save(commit=True)
                     

                return redirect('/')
           else:
                  form = ContactForm
            return render(request, template, context={"form":form})

To
def contact_form(request):
        template = 'contact.html'
        form = ContactForm(request.POST or None, request.Files or None)
               if form.is_valid():
                     form.save()
               else:
                      do something here
               return render(request, template, context={"form":form})

You are saving a form but calling back variable that holds unsaved version of the form.

Regards,
Mudassar



--

Cordialement, Coues Ludovic
+336 148 743 42
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Ali khan

unread,
Oct 1, 2016, 12:53:36 AM10/1/16
to django...@googlegroups.com
Thanks Mudassar,

Its working now.



Ali khan

unread,
Oct 1, 2016, 2:13:26 AM10/1/16
to django...@googlegroups.com
Ludovic I did what you told and that was reason it was not validating.
Thanks
Reply all
Reply to author
Forward
0 new messages