Thursday, September 29, 2016

Re: Django not saving form in DB from frontend

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.

No comments:

Post a Comment