Search code examples
djangodjango-formsdjango-templatesdjango-crispy-formsdjango-template-filters

Why is Django Crispy Forms throwing "module 'django.forms.forms' has no attribute 'BoundField'"


When I use the "|crispy" or "|as_crispy_field" filters on my form/fields, I get an error that the field has no attribute BoundField.

This was working fine, but I updated django/crispy forms, and I'm not sure whether I missed a trick? The form works fine without the filter.

EDIT: I'm using Django 3.1.2 and django-crispy-forms 1.8.1.

forms.py:

from django import forms
from django.utils import timezone
from bootstrap_modal_forms.forms import BSModalForm
from backgammon import models

class MatchForm(BSModalForm):
    date_played = forms.DateField(initial=timezone.now)

class Meta:
    model = models.Match
    fields = [
        'date_played',
        'winner',
        'score'
        ]

views.py

from django.contrib.auth.mixins import PermissionRequiredMixin
from bootstrap_modal_forms.generic import BSModalCreateView
from .forms import MatchForm

class MatchCreate(PermissionRequiredMixin, BSModalCreateView):
    permission_required = 'backgammon.add_match'
    template_name = 'backgammon/match_form.html'
    form_class = MatchForm
    success_message = 'Match saved.'
    success_url = reverse_lazy('backgammon-index')

match_form.html

{% load crispy_forms_tags %}

<div class="container bg-light">
  <form method="post">
    {% csrf_token %}

     <div class="modal-body">
       {% for field in form %}
         <div class="form-group{% if field.errors %} invalid{% endif %}">
           {{ field|as_crispy_field }}
         </div>
       {% endfor %}
     </div>

     <div class="modal-footer">
       {% if object %}<a class="btn btn-danger mr-auto" href="{% url 'match-delete' pk=object.pk %}">Delete</a>{% endif %}
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
       <button type="button" class="submit-btn btn btn-primary">Save</button>
     </div>
  </form>

</div>

Traceback:

Traceback (most recent call last):
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\core\handlers\base.py", line 202, in _get_response
    response = response.render()
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\defaulttags.py", line 211, in render
    nodelist.append(node.render_annotated(context))
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 988, in render
    output = self.filter_expression.resolve(context)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 698, in resolve
    new_obj = func(obj, *arg_vals)
  File "C:\Users\harla\Anaconda3\envs\djangoenv\lib\site-packages\crispy_forms\templatetags\crispy_forms_filters.py", line 98, in as_crispy_field
    if not isinstance(field, forms.BoundField) and settings.DEBUG:

Exception Type: AttributeError at /backgammon/match-create/
Exception Value: module 'django.forms.forms' has no attribute 'BoundField'

Local variables describe the "field" variable as <django.forms.boundfield.BoundField object at 0x0000021087C7E808>

Without the crispy filter, the page loads correctly:

Image of uncrispy django form loading correctly


Solution

  • As @ArakkalAbu commented, this was a version comparability issue. The version of django-crispy-forms in conda forge is out of date and incompatible with the latest version of Django.