对象没有属性 cleaned_data

Object has no attribute cleaned_data

我正在尝试在我的 Django 网络应用程序中填写一个表单,然后 post 它。它给我一个错误

object Application has no attribute cleaned_data

我在类似的问题上查看了 Stack Overflow,但其他人的解决方案与我的代码无关。这是我的观点:

def single_career(request, a_slug):
    a_slug = a_slug.strip('/')

    try:
        career = Career.objects.get(slug=a_slug)
    except:
        career = None

    if request.method == "POST":
        form = Application(request.POST, request.FILES)
        Post = True

        if form.is_valid():
            cleaned_data = Application.cleaned_data
            is_valid = True

            clean_first = cleaned_data['first_name']
            clean_last = cleaned_data['last_name']
            clean_email = cleaned_data['email']
            clean_phone = cleaned_data['phone']
            clean_file = cleaned_data['resume']
            clean_message = cleaned_data['message']
            date = datetime.datetime.now()

        else:
            is_valid = False

    else:
        form = Application()
        Post = False
        is_valid = False

    context = {'career': career, 'form': form, 'post': Post, 'is_valid': is_valid}

    template = 'overrides/careers.html'
    set_detail_context(request, context)

    return render_to_response(template, context, context_instance=RequestContext(request))

html:

<form action="" method="POST" enctype="multipart/form-data" class="application-form">
    {% csrf_token %}
    <div class="firstname">
        <p>FIRST NAME</p>
        {{ form.first_name }}
    </div>
    <div class="lastname">
        <p>LAST NAME</p>
        {{ form.last_name }}
    </div>
    <div class="email">
        <p>EMAIL</p>
        {{ form.email }}
    </div>
    <div class="phone">
        <p>PHONE</p>
        {{ form.phone }}
    </div>

    {#  form fileupload-input is hidden, and extra readonly text-input is there #}
    {#  to be able to override the styling of the fileupload-input button       #}
    <div>
        <p>ATATCH PDF RESUME</p>
        <input class="readonly" type="text" READONLY>
        <div class="resume">
            {{ form.resume }}
            <div>
                <a id="browse">BROWSE</a>
            </div>
        </div>
    </div>

    <div>
        <p>MESSAGE</p>
        {{ form.message }}
    </div>
    <button class="submit-button" type="submit">
        APPLY
    </button>
</form>

和表格 class:

from django import forms

class Application(forms.Form):
    first_name = forms.CharField(label="First Name", max_length=50)
    last_name = forms.CharField(label="Last Name", max_length=50)
    email = forms.EmailField(label="Email", max_length=80)
    phone = forms.CharField(label="Phone Number", max_length=30)
    resume = forms.FileField(label="Resume", max_length=1000)
    message = forms.CharField(label="Message", max_length=800, widget=forms.Textarea)

经过验证的表单是 form,而不是 Application

cleaned_data属于表单实例。你应该有:

if form.is_valid():
    cleaned_data = form.cleaned_data

您收到错误是因为您试图从表单 class.

中获取它
if form.is_valid():
    cleaned_data = Application.cleaned_data