dragon-fire-fly / developer_matcher

2 stars 1 forks source link

[BUG] Checkboxes not displaying correctly #17

Closed dragon-fire-fly closed 1 year ago

dragon-fire-fly commented 1 year ago

Describe the bug Whilst trying to change the multi-select panel to a series of checkboxes, the names of the programming languages are not displaying correctly.

To Reproduce Steps to reproduce the behavior:

  1. Go to profile page
  2. Click on 'edit profile'
  3. Scroll down to 'P language'
  4. See error

Expected behavior Checkboxes displayed with the name of the programming language next to them

Screenshots Before: p_languages_meta Now: p_language_boxes_bad

Desktop (please complete the following information):

Additional context This is the current code in app_user/forms.py:

class UserEditForm(forms.ModelForm):
p_language_queryset = ProgrammingLanguage.objects.all()
    p_language_choices = []
    for language in p_language_queryset:
        p_language_choices.append(language.language)
p_language = forms.MultipleChoiceField(choices=p_language_choices, widget=forms.CheckboxSelectMultiple, required=False)
dragon-fire-fly commented 1 year ago

A breakpoint was used to investigate the bug:

class UserEditForm(forms.ModelForm):
p_language_queryset = ProgrammingLanguage.objects.all()
    p_language_choices = []
    for language in p_language_queryset:
        breakpoint()
        p_language_choices.append(language.language)
    p_language = forms.MultipleChoiceField(choices=p_language_choices, widget=forms.CheckboxSelectMultiple, required=False)

pdb_debugging_checkboxes

"language" is correctly finding the p_language object (in this case "python"). dir(language) shows the possible methods for language and ".language" is present. language.language is correctly displaying "Python"

The problem is that the multiple choice field requires a 2-item tuple - the first item is the database reference and the second is what is displayed in the form. Reference: https://www.geeksforgeeks.org/multiplechoicefield-django-forms/

The following code was added to fix the issue:

class UserEditForm(forms.ModelForm):
p_language_queryset = ProgrammingLanguage.objects.all()
    p_language_choices = []
    for language in p_language_queryset:
        p_language_choices.append((language.language.lower(), language.language))
    p_language = forms.MultipleChoiceField(choices=p_language_choices, widget=forms.CheckboxSelectMultiple, required=False)

Which resulted in correct rendering of the checkboxes: checkboxes_fixed

dragon-fire-fly commented 1 year ago

Further bug related to this bug fix : the id (as an integer) of the programming language was expected instead of the name of the language: p_language_int_expected this was fixed by changing p_language_choices.append((language.language.lower(), language.language)) to p_language_choices.append((language.id, language.language))