jieter / django-tables2

django-tables2 - An app for creating HTML tables
https://django-tables2.readthedocs.io/en/latest/
Other
1.9k stars 429 forks source link

IndexError when verbose_name = '' #748

Open Liamhanninen opened 4 years ago

Liamhanninen commented 4 years ago

Sorry if this is already an issue. I'd actually like to try and fix this myself - I haven't done a pull request before. But I think I can fix this. In models.py when verbose_name equals an empty string ie.

opcreditsneeded = models.CharField(max_length=200,db_column='OpCreditsNeeded', blank=True, null=True, verbose_name = '')

I get an IndexError:

  File "D:\Code\StrandersProd\utilities\views_decoupled.py", line 133, in generic_dynamic_table
    MasterTable = tables.table_factory(current_model, fields=model_fields)
  File "C:\Users\liamh\AppData\Local\conda\conda\envs\stranders\lib\site-packages\django_tables2\tables.py", line 739, in table_factory
    return type(table)(class_name, (table,), table_class_attrs)
  File "C:\Users\liamh\AppData\Local\conda\conda\envs\stranders\lib\site-packages\django_tables2\tables.py", line 64, in __new__
    extra[field_name] = columns.library.column_for_field(field)
  File "C:\Users\liamh\AppData\Local\conda\conda\envs\stranders\lib\site-packages\django_tables2\columns\base.py", line 55, in column_for_field
    column = candidate.from_field(field)
  File "C:\Users\liamh\AppData\Local\conda\conda\envs\stranders\lib\site-packages\django_tables2\columns\base.py", line 427, in from_field
    return cls(verbose_name=ucfirst(verbose_name))
  File "C:\Users\liamh\AppData\Local\conda\conda\envs\stranders\lib\site-packages\django\utils\functional.py", line 195, in wrapper
    return func(*args, **kwargs)
  File "C:\Users\liamh\AppData\Local\conda\conda\envs\stranders\lib\site-packages\django\template\defaultfilters.py", line 46, in _dec
    return func(*args, **kwargs)
  File "C:\Users\liamh\AppData\Local\conda\conda\envs\stranders\lib\site-packages\django_tables2\utils.py", line 22, in ucfirst
    return s[0].upper() + s[1:]
IndexError: string index out of range

Would you consider this a bug/issue? Can I try to tackle it?

jieter commented 4 years ago

Yes, please feel free to open a PR adding a special case for empty strings ucfirst.