Friday, September 30, 2016

Re: Django not saving form in DB from frontend

Thanks Mudassar,

Its working now.



On Fri, Sep 30, 2016 at 8:42 PM, M Hashmi <mhashmi1979@gmail.com> wrote:
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



On Thu, Sep 29, 2016 at 2:18 PM, ludovic coues <couesl@gmail.com> wrote:
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.

2016-09-29 21:06 GMT+02:00 Ali khan <alipathan123123@gmail.com>:
> Appologies. In my view after else clause its ContactForm.
>
> On Thu, Sep 29, 2016 at 11:52 AM, Ali khan <alipathan123123@gmail.com>
> wrote:
>>
>> 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.
>>
>
> --
> 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-users@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

--
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-users@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/CAEuG%2BTaKjumkUsT_6Y4WVKkZqtG%3DyPE6G6bazDJu39WewtaZrA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

--
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-users@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/CANoUts5%3D%3DkVbJxgCf2MqjjQXkEhNqimzQfn2bJ7Vn%3D_k%3Divqyg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
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-users@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/CAAXvsY%3DLZO1dX%2BDhTGaZ9hkW5SpwpEC5Y43YDeSQTS20bS56Og%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment