Home > Enterprise >  How do I add a placeholder on a FileField in Django?
How do I add a placeholder on a FileField in Django?

Time:12-31

I tried creating placeholder by using the following code:

class ReceiptsForm(forms.ModelForm):

    class Meta:
        model = Receipts
        fields = ('receipt',)

    widgets = {

            'receipt' : forms.FileInput(attrs={'class': 'form-control', 'placeholder': 'Maximum 3 files allowed.'}),

        }

And then rendering by using the following snippet in the template:

<form action="." method="post" enctype="multipart/form-data">
   {% csrf_token %}
   {{ form | crispy }}

   <button type="submit" >Submit</button>
</form>

But still the placeholder text is not appearing besides the fileinput.

CodePudding user response:

What you want, is a help_text. Django defines this in regular Form.

class ReceiptsForm(forms.ModelForm):

    class Meta:
        model = Receipts
        fields = ('receipt',)
        help_texts = {
             'receipt': 'YOUR HELP TEXT HERE',
        }
        widgets = {
            'receipt' : forms.FileInput(attrs={'class': 'form-control'}),

        }

Not sure you need to add form-control CSS class if you are using crispy with BOOSTRAP_PACK ?

  • Related