Django:对象没有属性“cleaned_data”

问题描述 投票:0回答:1

我的 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”对象没有属性“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 %}

我收到以下错误: “ContactForm”对象没有属性“cleaned_data” 在“views.py”中——“if form.is_validate()”返回“False”..并且以下代码未执行。

python django attributeerror
1个回答
0
投票

问题似乎是 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>
© www.soinside.com 2019 - 2024. All rights reserved.