my models.py is Like this:
from django.db import modelsclass Form(models.Model): first_name = models.CharField(max_length=80) last_name = models.CharField(max_length=80) email = models.EmailField() date = models.DateField() occupation = models.CharField(max_length=80) # Magic Method def __str__(self): return f"{self.first_name}{self.last_name}"My forms.py is:
from django import formsclass ContactForm(forms.Form): first_name = forms.CharField(max_length=80) last_name = forms.CharField(max_length=80) email = forms.EmailField() date = forms.DateField() occupation = forms.CharField(max_length=80)And my views.py is:
from django.shortcuts import renderfrom .forms import ContactFormfrom .models import Formdef index(request): # To fetch data from webpage if request.method == "POST": form = ContactForm(request.POST) if form.is_valid(): first_name = form.cleaned_data["fname"] last_name = form.cleaned_data["lname"] email = form.cleaned_data["email"] date = form.cleaned_data["date"] occupation = form.cleaned_data["occupation"] print(first_name) # To store in database Form.objects.create(first_name=first_name, last_name=last_name, email=email, date=date, occupation=occupation) return render(request, "index.html")And I am getting the following error:'ContactForm' object has no attribute 'cleaned_data'In 'views.py' -- "if form.is_validate()" is returning "False"..and the following code is not executing.
Here is the html:
{% extends 'base.html' %}{% block content %}<h1 class="mt-4 mb-4">Job Application Form</h1><form method="post"> {% csrf_token %}<div class="form-group mb-4"><label for="fname">First Name:</label><br><input class="form-control" type="text" id="fname" name="fname" required></div><div class="form-group mb-4"><label for="lname">Last Name:</label><br><input class="form-control" type="text" name="lname" id="lname" required></div><div class="form-group mb-4"><label for="email">Email:</label><br><input class="form-control" type="email" name="email" id="email" required></div><div class="form-group mb-4"><label for="date">Available start date:</label><br><input class="form-control" type="date" name="date" id="date" required></div><div class="form-group mb-4"><label>Current Occupation:</label><br><div class="btn-group-vertical" id="occupation"><input class="btn-check form-control" type="radio" name="occupation" id="employed" value="employed" required><label class="btn btn-outline-secondary" for="employed">Employed</label><input class="btn-check form-control" type="radio" name="occupation" id="un_employed" value="un_employed" required><label class="btn btn-outline-secondary" for="un_employed">Unemployed</label><input class="btn-check form-control" type="radio" name="occupation" id="self_employed" value="self_employed" required><label class="btn btn-outline-secondary" for="self_employed">Self-Employed</label><input class="btn-check form-control" type="radio" name="occupation" id="student" value="student" required><label class="btn btn-outline-secondary" for="student">Student</label></div></div><button class="btn btn-secondary" type="submit">Submit</button><br><br> {% if messages %}<div class="alert alert-success"> {% for message in messages %} {{message}} {% endfor %}</div> {% endif %}</form>{% endblock %}And I am getting the following error:'ContactForm' object has no attribute 'cleaned_data'In 'views.py' -- "if form.is_validate()" is returning "False"..and the following code is not executing.