7
A
回答
9
這裏有一個片段here,它將當天設置爲1
(假設你有一個DateField
這個值將最終結束,你需要得到某種日子)。
的代碼是這樣的(以防萬一Django的片段消失):
import datetime
import re
from django.forms.widgets import Widget, Select
from django.utils.dates import MONTHS
from django.utils.safestring import mark_safe
__all__ = ('MonthYearWidget',)
RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
class MonthYearWidget(Widget):
"""
A Widget that splits date input into two <select> boxes for month and year,
with 'day' defaulting to the first of the month.
Based on SelectDateWidget, in
django/trunk/django/forms/extras/widgets.py
"""
none_value = (0, '---')
month_field = '%s_month'
year_field = '%s_year'
def __init__(self, attrs=None, years=None, required=True):
# years is an optional list/tuple of years to use in the "year" select box.
self.attrs = attrs or {}
self.required = required
if years:
self.years = years
else:
this_year = datetime.date.today().year
self.years = range(this_year, this_year+10)
def render(self, name, value, attrs=None):
try:
year_val, month_val = value.year, value.month
except AttributeError:
year_val = month_val = None
if isinstance(value, basestring):
match = RE_DATE.match(value)
if match:
year_val, month_val, day_val = [int(v) for v in match.groups()]
output = []
if 'id' in self.attrs:
id_ = self.attrs['id']
else:
id_ = 'id_%s' % name
month_choices = MONTHS.items()
if not (self.required and value):
month_choices.append(self.none_value)
month_choices.sort()
local_attrs = self.build_attrs(id=self.month_field % id_)
s = Select(choices=month_choices)
select_html = s.render(self.month_field % name, month_val, local_attrs)
output.append(select_html)
year_choices = [(i, i) for i in self.years]
if not (self.required and value):
year_choices.insert(0, self.none_value)
local_attrs['id'] = self.year_field % id_
s = Select(choices=year_choices)
select_html = s.render(self.year_field % name, year_val, local_attrs)
output.append(select_html)
return mark_safe(u'\n'.join(output))
def id_for_label(self, id_):
return '%s_month' % id_
id_for_label = classmethod(id_for_label)
def value_from_datadict(self, data, files, name):
y = data.get(self.year_field % name)
m = data.get(self.month_field % name)
if y == m == "0":
return None
if y and m:
return '%s-%s-%s' % (y, m, 1)
return data.get(name, None)
0
一個Python 3插件這裏https://djangosnippets.org/snippets/10522/樣品。
用法示例:
class myForm(forms.Form):
# ...
date = forms.DateField(
required=False,
widget=MonthYearWidget(years=xrange(2004,2010))
)
0
我碰到同樣的問題,今天來和通過CSS屬性去除白天場和設置1作爲對清理當天的值來解決它。
#id_my_date_field_day-button {
display: none;
}
我用的ModelForm與更新視圖,因此在我的領域這讓生活簡單一點,因爲我一向對my_date_field當天有效值有初始數據。
相關問題
- 1. 顯示月份和年份
- 2. Django DateField的日曆只顯示月份和年份?
- 3. angularjs ui-date只顯示月份年份
- 4. 只月份和年份
- 5. 只顯示月份和年份jQuery Datepick插件
- 6. 如何在DateTimePicker控件中只顯示月份和年份
- 7. 如何設置datepicker的日期只顯示月份和年份
- 8. 如何在wordpress文章中只顯示月份和年份
- 9. Django按月份和年份篩選
- 10. Django - 需要只能處理年份或年份和月份值的datetime字段
- 11. 我必須從輸入的月份/年份打印月份和年份到當前的月份和年份
- 12. 有月份和年份PHP
- 13. 結合年份和月份
- 14. 減去年份和月份
- 15. 按年份和月份
- 16. LINQ - 按年份和月份
- 17. 選擇年份和月份
- 18. SQL:按月份和年份
- 19. selectdatewidget設置默認月份和日期
- 20. 顯示日期,月份和年份代替當前的日期,月份和年份
- 21. AngularJS/AngularUI:Datepicker只顯示年份?
- 22. 如何按年份和月份排序和顯示React數組?
- 23. 匹配月份和年份Python和datetime
- 24. 顯示年份從當年開始到當月前一個月的月份
- 25. 從月到年和月份
- 26. excel如何顯示當前的月份和年份與空間
- 27. Excel - 僅顯示特定月份和年份的列
- 28. Bootprap的Datepicker角度指令:僅顯示月份和年份
- 29. Excel公式僅顯示月份和年份?
- 30. RichFaces日曆:僅顯示月份和年份
你是個聰明人!謝謝。 – ApPeL 2011-01-20 08:29:28