MCPcopy
hub / github.com/django/django / format_value

Method format_value

django/forms/widgets.py:1234–1257  ·  view source on GitHub ↗

Return a dict containing the year, month, and day of the current value. Use dict instead of a datetime to allow invalid dates such as February 31 to display correctly.

(self, value)

Source from the content-addressed store, hash-verified

1232 return context
1233
1234 def format_value(self, value):
1235 """
1236 Return a dict containing the year, month, and day of the current value.
1237 Use dict instead of a datetime to allow invalid dates such as February
1238 31 to display correctly.
1239 """
1240 year, month, day = None, None, None
1241 if isinstance(value, (datetime.date, datetime.datetime)):
1242 year, month, day = value.year, value.month, value.day
1243 elif isinstance(value, str):
1244 match = self.date_re.match(value)
1245 if match:
1246 # Convert any zeros in the date to empty strings to match the
1247 # empty option value.
1248 year, month, day = [int(val) or "" for val in match.groups()]
1249 else:
1250 input_format = get_format("DATE_INPUT_FORMATS")[0]
1251 try:
1252 d = datetime.datetime.strptime(value, input_format)
1253 except ValueError:
1254 pass
1255 else:
1256 year, month, day = d.year, d.month, d.day
1257 return {"year": year, "month": month, "day": day}
1258
1259 @staticmethod
1260 def _parse_date_fmt():

Callers 1

Calls 4

get_formatFunction · 0.90
groupsMethod · 0.80
matchMethod · 0.45
strptimeMethod · 0.45

Tested by 1