0

I build a dropdown category selection menu myself to make the search function of my django site more preceise and filterd. This is working pretty fine so far but I was not able to figure out how I can change the default value of my dropdown menu as it's currently showing "--------" and not something like "All" as default selected value.

base.html:

<div class="my-custom-dropdown">
   <a>{{ categorysearch_form.category }}</a>
</div>

search_context_processor.py:

def categorysearch_form(request):
    form = SearchForm()
    return {'categorysearch_form': form}

forms.py

class SearchForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['category']

    def __init__(self, *args, **kwargs):
        kwargs.setdefault('label_suffix', '')
        super(SearchForm, self).__init__(*args, **kwargs)
        self.fields['category'].required = False
        self.fields['category'].initial = 'All'

does smb. has any idea how this can be accomplished? using the content: parameter at my css only has a very limited bennefit as the lable "All" always gets placed outside of the actual selection box.

thanks for reading :)

3 Answers3

1

The empty choice is displayed as "--------". To display something else (maybe "All") in place of "--------", we can add an empty_label in the ModelForms init method, like below -

self.fields['category'].empty_label = "All"

You can check this doc as well.

Ejaz
  • 1,504
  • 3
  • 25
  • 51
  • Yes this is working :D Thanks for this awesome hint. Never had the idea to use empty_label at this point –  Mar 15 '20 at 11:28
0

you can add initial value

in your form

name = forms.CharField(initial='Your name')
bmons
  • 3,352
  • 2
  • 10
  • 18
  • No im running into the following: File "/venv/lib/python3.8/site-packages/django/forms/models.py", line 296, in __init__ object_data.update(initial) ValueError: dictionary update sequence element #0 has length 1; 2 is required –  Mar 15 '20 at 10:42
  • see the link https://stackoverflow.com/questions/17610732/error-dictionary-update-sequence-element-0-has-length-1-2-is-required-on-dj – bmons Mar 15 '20 at 10:46
  • Okay, but just to make your response clear, im adding this to my forms.py and not to my context processor, right? Because you have written "in your form". Thanks so far :) –  Mar 15 '20 at 10:51
  • Beside i using a model form and not forms.Form. The link you have send does not seem to deal with this or is it simply not possible in that case? –  Mar 15 '20 at 10:52
  • can do it on model form too see this answer https://stackoverflow.com/questions/5864869/set-default-value-for-dropdown-in-django-forms – bmons Mar 15 '20 at 10:58
0

If category is a choice field in Post model and one of the choices is 'All', then you can set the initial value of the category to 'All' like this

class SearchForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ['category']

    def __init__(self, *args, **kwargs):
        kwargs.setdefault('label_suffix', '')
        super(SearchForm, self).__init__(*args, **kwargs)
        self.fields['category'].required = False
        self.fields['category'].initial = 'All'
danish_wani
  • 862
  • 9
  • 14
  • Well this also has no effect, I dont get behind this as it seems actually quite simple to implement. I'm starting to think that this is a CSS related issue, added my css accordingly –  Mar 15 '20 at 11:15