Django: у объекта нет атрибута 'cleaned_data'

Мой models.py выглядит следующим образом:

from django.db import models


class 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}"

Мой файл forms.py имеет следующий вид:

from django import forms

class 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)

А мой views.py выглядит так:

from django.shortcuts import render
from .forms import ContactForm
from .models import Form

def 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")

И я получаю следующую ошибку: 'ContactForm' object has no attribute 'cleaned_data' В 'views.py' -- "if form.is_validate()" возвращает "False"... и следующий код не выполняется.

Вот 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 %}

Похоже, что проблема заключается в том, что имена полей формы в вашем HTML не совпадают с именами полей в вашей форме Django. В HTML-форме вы используете fname и lname, а в Django-форме - first_name и last_name.

Вам нужно настроить вашу HTML-форму так, чтобы имена полей совпадали с именами полей в вашей Django-форме.

Попробуйте это:

<div class="form-group mb-4">
    <label for="fname">First Name:</label><br>
    <input class="form-control" type="text" id="fname" name="first_name" required>
</div>
<div class="form-group mb-4">
    <label for="lname">Last Name:</label><br>
    <input class="form-control" type="text" name="last_name" id="lname" required>
</div>
Вернуться на верх