I have a fairly simple model form with a 'date_of_birth' input value of type datefield(). For whatever reason, I cannot get it to display as a widget and can only get it to display as a text input.
Here is my form code: from django import forms from . import models from django.contrib.admin import widgets
class CreateNewPatient(forms.ModelForm):
class Meta:
model = models.PatientInfo
fields = ['first_name', 'nickname','last_name',
'date_of_birth', 'school_grade', 'sex', 'school']
Here is my model:
class PatientInfo(models.Model):
#first name, last name,nickname, and date of birth
first_name = models.CharField(max_length=100)
nickname = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100)
date_of_birth = models.DateField(auto_now=False, auto_now_add=False)
school_grade = models.CharField(max_length=15, blank=True)
Here is my template. It is based on a base template and is using the bootstrap4 plugin:
{% block content %}
<!--Form Container-->
<div class="container">
<form action="{% url 'patientRecords:new_patient' %}" method="post"
class="form">
{% csrf_token %}
{% bootstrap_form form %}
{% buttons %}
<button type="submit" class="btn btn-primary">Submit</button>
{% endbuttons %}
</form>
</div>
{% endblock %}
Like I said, the form displays the date of birth field as a text box. How do I make it display as the normal django date widget with bootstrap styling?
class Meta:
model = CreateNewPatient
fields = ['first_name', 'nickname','last_name',
'date_of_birth', 'school_grade', 'sex', 'school']
widgets = {
'date_of_birth': forms.DateInput(format=('%m/%d/%Y'), attrs={'class':'form-control', 'placeholder':'Select a date', 'type':'date'}),
}
That's how I got mine to work. Hope it helps!
should look something like this:enter image description here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With