djangowidgetdatafield

How to show from database to Django Data Field?


I select date using DateField and insert date value using this.

 form = DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES))

and then, I want to show selected value using DateField(form) in HTML from database

 (when create profileForm())
 Birth: selectbox(------), selectbox(------), selectbox(------) 
 : solved

 (if edit profile page using profileForm())
  Birth: selectbox(1987) selectbox(10) selectbox(25)
 -> I want it

How to insert value into DataField, and show selectDateWidget?


Solution

  • Your problem is not too clear.

    You should post more information from you forms.py and your views.py. Are you using forms.Form or forms.ModelForm?

    Either way I think your question is about using initial data in your form.

    Note that this field in particular accepts a python datetime object (see: http://docs.python.org/2/library/datetime.html).

    https://docs.djangoproject.com/en/dev/ref/forms/api/#dynamic-initial-values Use is like this:

    f = ContactForm(initial={'my_date_field': datetime.datetime.now()})
    

    If you are using ModelForm is is like this:

    f = ContactForm(instance=Contact.objects.get(pk=1))
    

    This will initialise all the values from the models (and thus database).