How to get a list of months using python
This is a quick-and-dirty hack that came to my mind this morning. I needed a list where each element is a tuple containing (month number, month name). This little snippet will do the trick:
months_choices = []
for i in range(1,13):
months_choices.append((i, datetime.date(2008, i, 1).strftime('%B')))
The result will be something like:
>>> months_choices = []
>>> for i in range(1,13):
... months_choices.append((i, datetime.date(2008, i, 1).strftime('%B')))
...
>>> months_choices
[(1, 'January'), (2, 'February'), (3, 'March'), (4, 'April'), (5, 'May'), (6, 'June'), (7, 'July'), (8, 'August'), (9, 'September'), (10, 'October'), (11, 'November'), (12, 'December')]
>>>
The name of the month will be translated to you locale configuration (according to python documentation, you can take a look there to replace that with, for example, the abbreviated name). This is a perfect list to be used as the default list of options with a django ChoiceField or MultipleChoiceField form field ;D.