“Django错误”;XXXForm";对象没有属性“;XXX“;

“Django错误”;XXXForm";对象没有属性“;XXX“;,django,django-forms,Django,Django Forms,我使用Django构建web应用程序,当我提交表单时,出现错误: AttributeError at /CustomerInfo/ 'CustomerForm' object has no attribute 'first_name' 项目名称为zqxt_views,应用程序名称为calc。 我在calc文件夹中创建了一个名为forms.py的文件,请参见下面的: calc/forms.py: from django import forms class CustomerForm(forms

我使用Django构建web应用程序,当我提交表单时,出现错误:

AttributeError at /CustomerInfo/
'CustomerForm' object has no attribute 'first_name'
项目名称为zqxt_views,应用程序名称为calc。 我在calc文件夹中创建了一个名为forms.py的文件,请参见下面的:
calc/forms.py:

from django import forms

class CustomerForm(forms.Form):
      customer_id = forms.IntegerField(label="Customer ID")
      first_name = forms.CharField(label="First Name", max_length=30)
      last_name = forms.CharField(label="Last Name", max_length=30)
calc/views.py:

# -*- coding: utf-8 -*-
#from __future__ import unicode_literals

#from django.shortcuts import render

from django.shortcuts import render
from django.http import HttpResponse
from django.http import HttpResponseRedirect
import MySQLdb
from calc.models import Customer
from calc.forms import CustomerForm
from django.db import connection

...

def show_save_customer(request):
    # if this is a POST request we need to process the form database
    if request.method == 'POST':
        # create a form instance and populate it with data from the request:
        form = CustomerForm(request.POST)
        cursor = connection.cursor()
        query = """ insert into customers (first_name, last_name) values (%s, %s) """
        cursor.execute(query, [form.first_name, form.last_name])
        # check whether it's valid:
        if form.is_valid():
            #process the data
            return HttpResponseRedirect('AddressTableMaintain/');
    else:
        form = CustomerForm()

    return render(request, 'CustomerInfo.html', {'form': form})

# Create your views here.
表单页面如下所示:
calc/templates/CustomerInfo.html:

{% extends 'base.html' %}

{% block title %} Add and Show Customer details {% endblock %}

{% block content %}
<style>
table, th, td {
    border: 1px solid black;
    border-collapse: collapse;
}
</style>
<form action="{% url 'show_save_customer' %}" method="post">
{% csrf_token %}
<table width="50%" align="center">
    <tr>
        <td>Customer ID</td>
        <td>
            {{ form.customer_id }}

        </td>
    </tr>
    <tr>
        <td>First Name</td>
        <td>
            {{ form.first_name }}

        </td>
    </tr>

    <tr>
        <td>Last Name</td>
        <td>
            {{ form.last_name }}
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center"><input type="submit" value = "OK" /></td>
    </tr>
</table>
</form>
{% endblock %}
{%extends'base.html%}
{%block title%}添加并显示客户详细信息{%endblock%}
{%block content%}
表,th,td{
边框:1px纯黑;
边界塌陷:塌陷;
}
{%csrf_令牌%}
客户ID
{{form.customer_id}
名字
{{form.first_name}
姓
{{form.last_name}
{%endblock%}

填写一些数据并单击“确定”按钮后,我总是会看到开头提到的错误,有人能告诉我代码有什么问题吗?

事实上,表单不会将字段作为属性公开。您需要通过
form.cleaned\u data
访问数据-在调用
form.is\u valid()
后应该这样做

但是,请注意,除非您有很好的理由,否则您应该避免原始SQL查询,并使用Django模型层进行查询。在这种情况下,ModelForm更合适,因为它将使用一个简单的
form.save()
为您创建和保存实例

还要注意的是,当表单无效时,您对表单执行任何操作都会显示错误。有多种方法可以做到这一点,但至少你应该把
{{form.errors}}
放在那里的某个地方

    form = CustomerForm(request.POST)
    cursor = connection.cursor()
    # check whether it's valid:
    if form.is_valid():
        query = """ insert into customers (first_name, last_name) values (%s, %s) """
        cursor.execute(query, [form.cleaned_data['first_name'], form.cleaned_data['last_name']])
        return HttpResponseRedirect('AddressTableMaintain/');