首页 > 解决方案 > Django Forms AttributeError 'NoneType' object has no attribute 'disabled'

问题描述

I keep having trouble with a custom Django form that I am trying to implement. I really didn't know any way for getting the data from the frontend besides trying to pull the data by force through request.POST.get(), but once I started doing that I ran into an AttributeError that kept saying 'NoneType' object has no attribute 'disabled', and the error referenced the form.save() line as the problem in my views.py. Here is the code for the views.py

@login_required
def volunteer_instance_creation(request):
    form = VolunteerInstanceForm(request.POST or None, request.FILES)
    values_dict = {
        "organization": request.POST.get('organization'),
        "supervisor_Name": request.POST.get('supervisor_Name'),
        "supervisor_Phone_Number": request.POST.get('supervisor_Phone_Number'),
        "volunteering_Duration": request.POST.get('volunteering_Duration'),
        "volunteering_Date": request.POST.get('volunteering_Date'),
        "description": request.POST.get('description'),
        "scanned_Form": request.POST.get('scanned_Form'),
    }
    for i in values_dict:
        form.fields[i] = values_dict[i]
    if form.is_valid:
        obj = form.save(commit = False)
        obj.student_id = get_student_id(str(User.objects.get(pk = request.user.id)))
        obj.grad_year = get_grad_year(str(User.objects.get(pk = request.user.id)))
        obj.save()
        return redirect("submission_success")
    return render(request, "members_only/yes_form.html")

Here is my models.py

class VolunteerInstance(models.Model):
student_id = models.CharField(
    blank = True, 
    editable = True, 
    default = 'No Student ID Provided',
    max_length = 255
)
grad_year = models.CharField(
    blank = True, 
    max_length = 255,  
    default = 'No Graduation Year Provided', 
    editable = True
)
organization = models.CharField(
    blank = False,
    max_length = 255
)
description = models.TextField(
    blank = False, 
)
supervisor_Name = models.CharField(
    blank = False, 
    max_length = 255
)
supervisor_Phone_Number = models.CharField(
    blank = True, 
    null = True, 
    max_length = 10
)
volunteering_Date = models.DateField()
volunteering_Duration = models.FloatField()
scanned_Form = models.FileField(
    null = True,
    blank = True, 
    upload_to = 'scanned_files', 
)
approved = models.BooleanField(
    default = False, 
    blank = True
)

And here is the template that has been causing problems for me.

<form action = '.' method = "POST"> 
    {% csrf_token %}
    <div class="form-group row">
        <label class="col-sm-12 col-md-2 col-form-label">Nonprofit Agency</label>
        <div class="col-sm-12 col-md-10">
            <input class="form-control" type="text" placeholder = "Nonprofit Agency" name = "organization"> 
        </div>
    </div>
    <div class="form-group row">
        <label class="col-sm-12 col-md-2 col-form-label">Supervisor Name</label>
        <div class="col-sm-12 col-md-10">
            <input class="form-control" placeholder="Johnny Brown" type="text" name = "supervisor_Name">
        </div>
    </div>
    <div class="form-group row">
        <label class="col-sm-12 col-md-2 col-form-label">Organization Phone Number</label>
        <div class="col-sm-12 col-md-10">
            <input class="form-control" value="" type="tel" placeholder="Phone number without dashes or parenthesis" name = "supervisor_Phone_Number">
        </div>
    </div>
    <div class="form-group row">
        <label class="col-sm-12 col-md-2 col-form-label">Volunteering Duration</label>
        <div class="col-sm-12 col-md-10">
            <input class="form-control" placeholder="Duration in hours" type="number" name = "volunteering_Duration">
        </div>
    </div>
    <div class="form-group row">
        <label class="col-sm-12 col-md-2 col-form-label">Service Date</label>
        <div class="col-sm-12 col-md-10">
            <input class="form-control date-picker" placeholder="Select Date" type="text" name = "volunteering_Date">
        </div>
    </div>
    <div class="form-group">
        <label>Service Description</label>
        <textarea class="form-control"></textarea>
        
    </div>
    <div class="form-group">
        <label>Upload PDF File</label>
        <input type="file" class="form-control-file form-control height-auto" name = scanned_Form>
    </div>
    <div class="custom-control custom-checkbox mb-5">
        <input type="checkbox" class="custom-control-input" id="customCheck1" disabled = "" name = "approved"> 
        <label class="custom-control-label" for="customCheck1">Approved by Admin</label>
    </div>
    <input type = "submit" class = "btn btn-success" value = "Submit" name = "submit"/> 
</form>
{% if form.non_field_errors %}
        {% for error in form.non_field_errors %}
            <p class="text-danger">{{ error }}</p>
        {% endfor %}
{% endif %}

My forms.py

today = dt.datetime.today()

class VolunteerInstanceForm(forms.ModelForm):
volunteering_Date = forms.DateField(
    input_formats = DATE_INPUT_FORMATS, 
    widget = forms.TextInput(
        attrs = {
            "placeholder":"month/day/year"
        }
    )
)
volunteering_Duration = forms.FloatField(
    validators=[
        MinValueValidator(
            limit_value = 0.5,
            message = 'Minimum value in this field must be 0.5',
        )
    ]
)
supervisor_Phone_Number = forms.CharField(
    label = 'Supervisor Phone Number',
    widget = forms.TextInput(
        attrs = {
            "placeholder": "Please the 10 digit phone number exculding dashes and parenthesis"
        }
    ), 
    validators=[
        RegexValidator(
            regex = '^(\d\d\d\d\d\d\d\d\d\d)$', 
            message = "Please enter the phone number excluding any dashes and parenthesis", 
            code = "invalid_phone_number"
        )
    ]
)
approved = forms.BooleanField(
    label = 'Approved by Supervisor', 
    initial = False, 
    required= False,
)

def get_service_date(self):
    date = self.cleaned_data.get('date')
    return str(date)

scanned_Form = forms.FileField(
    label = "Please upload scanned YES Form here", 
    help_text = "max. 50 megabytes", 
)

class Meta:
    model = VolunteerInstance
    fields = [
        "organization", 
        "description", 
        "supervisor_Name", 
        "supervisor_Phone_Number", 
        "volunteering_Date", 
        "volunteering_Duration", 
        "scanned_Form", 
        "approved"
    ]

标签: djangodjango-modelsdjango-viewsdjango-formsdjango-templates

解决方案


推荐阅读