1

Im trying to create a form that will show a list of checkboxes based on a models items. Then also to be able to filter this list if required.

However I am getting the below error and am not sure as to why?

error:

File "/usr/local/lib/python3.6/site-packages/django/forms/forms.py" in errors
  174.         if self._errors is None:

Exception Type: AttributeError at /sites/site/auto_gen_subnets/7
Exception Value: 'AutoSubnetForm' object has no attribute '_errors'

forms.py

class AutoSubnetForm(forms.Form):
    subnet_type_data = SiteTypes.objects.all()

    def __init__(self, *args, **kwargs): 
        self.site_type = kwargs.pop("site_type")
        # get site type if set and filter against it
        if self.site_type:
            subnet_type_data = SiteTypes.objects.filter(site_type=self.site_type)

    # create list for types
    subnet_types = []
    for stype in subnet_type_data:
        # add tuple for each type
        subnet_types.append((stype.id,stype.site_type))

    subnets = forms.ChoiceField(
            choices=subnet_types,
            widget = forms.Select(
            attrs = {'class': 'form-control'}
        )
    )

views.py:

@login_required
@user_passes_test(lambda u: u.has_perm('config.add_subnet'))  
def auto_gen_subnets(request, site_id):
    #generate_subnets(site_id)
    from config.models import SubnetTypes   
    site_data = get_object_or_404(SiteData.objects.select_related('site_type'),pk=site_id)
    subnets = None
    if request.method == 'GET':
        form = AutoSubnetForm(site_type=site_data.site_type)
    else:
        # A POST request: Handle Form Upload
        form = AutoSubnetForm(request.POST)
        # If data is valid, proceeds to create a new post and redirect the user
        if form.is_valid():
            subnets = form.cleaned_data['subnets']

    return render(request, 'sites/generate_subnets.html', {
        'data': subnets,
        'subnet_form': form,  
        'SiteName' : site_data.location,
        'SiteID' : site_id, 
        }
    )

1 Answer 1

4

You override the init method. So you should return it to your superclass.

def __init__(self, *args, **kwargs): 
    self.site_type = kwargs.pop("site_type")
    # get site type if set and filter against it
    if self.site_type:
        subnet_type_data = SiteTypes.objects.filter(site_type=self.site_type)

    super(AutoSubnetForm, self).__init__(*args, **kwargs)

You do not return your _errors. So it does not know about any other data except the ones you provide while overriding. If you want all you should return it to superclass. That should cause it. The code above should fix it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.