1

I created my own widget which inherited from the Widget class which functions much like a Select widget. Only it was causing the page to take ages to load. Thus started a debugging session which has resulted in the following discovery. When my form uses the native Select widget - which I've copied into one my own python modules so i can change it - it loads fast. According to the django debug-toolbar the page load is about 500ms. But when I change the template_name and option_template_name to my own templates which have been changed to include exactly the same code it takes ages again like my original custom widget, about 5500ms.


# Here is the beginning of the Select Widget class which i've copeied into my own widgets.py module.

class Select(ChoiceWidget):
    input_type = 'select'
    template_name = 'django/forms/widgets/select.html'
    option_template_name = 'django/forms/widgets/select_option.html'
    add_id_index = False
    checked_attribute = {'selected': True}
    option_inherits_attrs = False

Then i make two changes only - as outlined above.


class Select(ChoiceWidget):
    input_type = 'select'
    #template_name = 'django/forms/widgets/select.html'
    #option_template_name = 'django/forms/widgets/select_option.html'
    add_id_index = False
    checked_attribute = {'selected': True}
    option_inherits_attrs = False

    # input_type = 'select'
    template_name = "purchases/input_with_dropdown.html"
    option_template_name = "purchases/dropdown_options.html"
    # option_inherits_attrs = False
    # checked_attribute = {'data-selected': True}
    # add_id_index = False


It now takes ages to load.

And here is the html for input_with_dropdown.html. Which is exactly the same as select.html from django.


<select name="{{ widget.name }}"{% include "django/forms/widgets/attrs.html" %}>{% for group_name, group_choices, group_index in widget.optgroups %}{% if group_name %}
  <optgroup label="{{ group_name }}">{% endif %}{% for option in group_choices %}
  {% include option.template_name with widget=option %}{% endfor %}{% if group_name %}
  </optgroup>{% endif %}{% endfor %}
</select>

And here is dropdown_options.html. Again the same as select_options.html from django.

<option value="{{ widget.value|stringformat:'s' }}"{% include "django/forms/widgets/attrs.html" %}>{{ widget.label }}</option>

I can't understand how to even debug this further.

My only conclusion is therefore i am referencing the templates in the wrong way so it is taking ages to find them? Although this doesn't ring true.

SOLVED -

Problem is with the django-debug-toolbar. Whole evening wasted thanks to that app.

Similar problem reported here - https://github.com/jazzband/django-debug-toolbar/issues/910

user1849962
  • 1,273
  • 1
  • 11
  • 16
  • Instead of deleting this question I will leave if it helps anybody else ... the problem is nothing to do with django ... rather, django-debug-toolbar .... DOH ! Looks like somebody found a similar problem here - https://github.com/jazzband/django-debug-toolbar/issues/910 – user1849962 May 27 '20 at 22:01

0 Answers0