2

Django 1.10

I have a model in fields of which I prefer null=False. But those default values become default values in the form. This is dangerous: blank=False validation no more available.

I have created a form with two purposes:

  1. Clear default values.
  2. Show the placeholder.

But the result is that html renders without placeholders. I have also tried TextInput without success. Could you help me understand how to cope with this problem?

class Masterphoto(models.lModel):
    dated_from = models.DateField(blank=False, null=False, default=datetime.date(2000, 1, 1))
    dated_to = models.DateField(blank=False, null=False, default=datetime.date(2000, 1, 1))

class MasterphotoForm(ModelForm):
    dated_from = forms.DateField()
    dated_to = forms.DateField()


    class Meta:
        model = Masterphoto
        exclude = []
        widgets = {
            'dated_from': forms.DateInput(attrs={'placeholder': 'YYYY-MM-DD'}),
            'dated_to': forms.DateInput(attrs={'placeholder': 'YYYY-MM-DD'}),         
        }
Michael
  • 4,273
  • 3
  • 40
  • 69

1 Answers1

2

Have you tried using:

forms.DateField(widget=forms.DateInput(attrs={'placeholder': 'YYYY-MM-DD', 'required': 'required'}))

We do something very similar to this for our forms where we want to show a sample date for format and it doesn't ruin the form validation in the event someone tries to insert a blank value.

Michael Platt
  • 1,297
  • 12
  • 25