django Contact Form data not being saved

137 Views Asked by At

I am building a website for my brother and it needs a contact form, the HTML template had one pre built so i custom build a model form based off what was included in the template, so far it all works, except when you press submit on the website the data isnt saved to the database and isnt viewable in admin.

but the template renders out all fine and the page redirects when you press submit, it also wont allow you to submit if the required fields arent there or contain invalid entries.

im getting this in the terminal ' "POST / HTTP/1.1" 200 15649'

so it looks like a successful post request, yet nothing in the database? what am i missing here?

index.html

<form id="contact" action="" method="post">
                      {% csrf_token %}
                      {{ form.as_p }}
                      
                        <div class="row">
                          <div class="col-md-12">
                            <h2>Contact me</h2>
                          </div>
                          <div class="col-md-6">
                            <fieldset>
                              <input name="name" type="text" class="form-control" id="name" placeholder="Your name..." required="">
                            </fieldset>
                          </div>
                          <div class="col-md-6">
                            <fieldset>
                              <input name="email" type="text" class="form-control" id="email" placeholder="Your email..." required="">
                            </fieldset>
                          </div>
                          <div class="col-md-12">
                            <fieldset>
                              <textarea name="message" rows="6" class="form-control" id="message" placeholder="Your message..." required=""></textarea>
                            </fieldset>
                          </div>
                          <div class="col-md-12">
                            <fieldset>
                              <button type="submit" id="form-submit" class="button">Send Now</button>
                            </fieldset>
                        </div>
                    </form>

forms.py

from django import forms
from .models import Contact


class ContactForm(forms.ModelForm):
    
    class Meta:

        model = Contact
        fields = ['name', 'email', 'message']


views.py

from django.shortcuts import render
from .forms import ContactForm

# Create your views here.
def home(request):
    return render(request, 'rattlesnake/index.html')



def contactView(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
            
    else:
        form = ContactForm()    
    return render(request, 'rattlesnake/index.html', {'form':form})

models.py

from django.db import models

class Contact(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
    message = models.TextField() 


    def __str__(self):
        return self.name 

urls.py

from django.urls import path
from . import views



urlpatterns = [
    path('', views.home, name='home'),
    path('contact/', views.contactView, name='contact'),
]
1

There are 1 best solutions below

2
Kikito On

You're getting 200 but the data is not saved. I think it's the case where form is not valid which is not handled in your view.

def contactView(request):
    if request.method == 'POST':
        form = ContactForm(request.POST)
        if form.is_valid():
            form.save()
        # else <- should be handled and debugged.
            
    else:
        form = ContactForm()    
    return render(request, 'rattlesnake/index.html', {'form':form})

Adding some logger and handling the else case of your View code could give you more information on what's not working in your form.

Hope this helps!