Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django form dropdown list of numbers

Tags:

i am new to django and i want to make a simple form, according to the doc i can make a form using forms module from django

from django import forms  class CronForm(forms.Form):     name = forms.CharField(max_length=100)     email = forms.EmailField(required=False, label='Your e-mail address')     message = forms.CharField(widget=forms.Textarea)      def clean_message(self):         message = self.cleaned_data['message']         num_words = len(message.split())         if num_words < 4:             raise forms.ValidationError("Not enough words!")         return message 

what i want to know is how to create a dropdown list of days in month i.e from 1 to 31?

some have done it using javascript in their form template, can this be done in django?

like image 220
krisdigitx Avatar asked Jan 14 '12 01:01

krisdigitx


1 Answers

You're looking for a ChoiceField which renders as a select html element by default. https://docs.djangoproject.com/en/dev/ref/forms/fields/#choicefield

class CronForm(forms.Form):     days = forms.ChoiceField(choices=[(x, x) for x in range(1, 32)]) 
like image 169
Yuji 'Tomita' Tomita Avatar answered Sep 23 '22 17:09

Yuji 'Tomita' Tomita