The Law
The Law

Reputation: 41

My form gets Submitted but no data is being added in database django

I am trying to make a website's contact page using django where client enters data and it gets submitted in database, the form gets submitted the project runs without errors and yet no data gets added in the db. Here's my views.py file

from datetime import datetime
from firstapp.models import Contact

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

def apply(request):
    return render(request,'apply.html')

def about(request):
    return render(request,'about.html')

def socials(request):
    return render(request,'social.html')

def contact(request):
    if request.method == "POST":
        name = request.POST.get("name")
        email = request.POST.get("email")
        subject = request.POST.get("subject")
        message= request.POST.get("message")
        contact=Contact(name=name,email=email,subject=subject,message=message,date=datetime.today())
        contact.save
    
    return render(request,'contact.html')

here is my contact.html

{% block title %}Contact {% endblock title %}
{% block body %}
<h2 align="center">CONTACT US </h2>
<div class="container-md">
<form method="POST" action="/contact/">
{% csrf_token %}
<div class="form-group">
    <label for="exampleFormControlInput1">Name</label>
    <input type="text" class="form-control" id="exampleFormControlInput1" name="name" placeholder="John Smith">
  </div>
  <div class="form-group">
    <label for="exampleFormControlInput1">Email address</label>
    <input type="email" class="form-control" id="exampleFormControlInput1" name="email" placeholder="[email protected]">
  </div>
  <div class="form-group">
    <label for="exampleFormControlInput1">Subject</label>
    <input type="text" class="form-control" id="exampleFormControlInput1" name="subject" placeholder="Business
    | Suggestions | Query | Complaint | Other">
  </div>
  <div class="form-group">
    <label for="exampleFormControlTextarea1">Message</label>
    <textarea class="form-control" name="message" id="exampleFormControlTextarea1" rows="3"></textarea>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>


</form>
</div>

{% endblock body%}

And here's my models.py

from django.db import models

# Create your models here.

class Contact(models.Model):
    name = models.CharField(max_length=50)
    email =models.EmailField(max_length=254)
    subject=models.CharField(max_length=10)
    message=models.CharField(max_length=1000)
    date=models.DateField()

Upvotes: 0

Views: 109

Answers (1)

Alexander Yudkin
Alexander Yudkin

Reputation: 472

As I wrote at the comments before, you have forgotten to type brackets following the save: contact.save() instead of contact.save.

It would be better and more beautiful if you do it like this:

def contact(request):
    if request.method == "POST":
      Contact.objects.create(**request.POST)
    return render(request,'contact.html')

Upvotes: 1

Related Questions