Repository: zostera/django-bootstrap4

Issue:
'NoneType' object has no attribute 'attrs' when upgrading to 2.0.0
I just tried upgrading my project to django-bootstrap 2.0.0, and encountered the following error when loading websites.

```
Traceback (most recent call last):
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/response.py", line 105, in render
    self.content = self.rendered_content
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 171, in render
    return self._render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/test/utils.py", line 95, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/test/utils.py", line 95, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/test/utils.py", line 95, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 936, in render
    bit = node.render_annotated(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/base.py", line 903, in render_annotated
    return self.render(context)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/django/template/library.py", line 192, in render
    output = self.func(*resolved_args, **resolved_kwargs)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/bootstrap4/templatetags/bootstrap4.py", line 627, in bootstrap_field
    return render_field(*args, **kwargs)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/bootstrap4/forms.py", line 39, in render_field
    return renderer_cls(field, **kwargs).render()
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/bootstrap4/renderers.py", line 84, in render
    return mark_safe(self._render())
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/bootstrap4/renderers.py", line 515, in _render
    html = self.post_widget_render(html)
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/bootstrap4/renderers.py", line 363, in post_widget_render
    html = self.list_to_class(html, "checkbox")
  File "/home/rixx/.local/share/virtualenvs/pretalx/lib/python3.8/site-packages/bootstrap4/renderers.py", line 341, in list_to_class
    label.input.attrs["class"] = label.input.attrs.get("class", []) + ["form-check-input"]
AttributeError: 'NoneType' object has no attribute 'attrs'
```

This is a direct regression to #181, where django-bootstrap4 assumes that all inputs have to be inside labels (which is not idiomatic or at least not required in HTML).


Discussion:
Thanks. Apparently there is no test on this regression. Will look into that.

The major version dump in 2 is because of the drop n support fro Python and Django versions. You should be fine with `django-bootstrap4<2`.

Resolving patch:
diff --git a/src/bootstrap4/renderers.py b/src/bootstrap4/renderers.py
--- a/src/bootstrap4/renderers.py
+++ b/src/bootstrap4/renderers.py
@@ -338,7 +338,10 @@ def list_to_class(self, html, klass):
         if enclosing_div:
             for label in enclosing_div.find_all("label"):
                 label.attrs["class"] = label.attrs.get("class", []) + ["form-check-label"]
-                label.input.attrs["class"] = label.input.attrs.get("class", []) + ["form-check-input"]
+                try:
+                    label.input.attrs["class"] = label.input.attrs.get("class", []) + ["form-check-input"]
+                except AttributeError:
+                    pass
         return str(soup)
 
     def add_checkbox_label(self, html):


Test patch: