Opened 17 years ago
Closed 17 years ago
#6112 closed (duplicate)
SelectDateWidget don't render correctly
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | ||
Cc: | Triage Stage: | Unreviewed | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
SelectDateWidget don't render correctly
Attachments (4)
Change History (11)
by , 17 years ago
Attachment: | selectdate.diff added |
---|
by , 17 years ago
Attachment: | selectdate_right.diff added |
---|
comment:1 by , 17 years ago
comment:2 by , 17 years ago
The "value" from instance, then the "value" is datetime.date instance.
The "value" from request.POST, then the "value" is unicode string.
force_unicode just make sure the "value". make it render correctly, no matter where it come from.
comment:3 by , 17 years ago
Actually if you are using SelectDateWidget it's suppose to have only numbers (represented as string; but just numbers) from request.POST. I mean, value_from_datadict is returning '%s-%s-%s' % (y, m, d). And those y m and d MUST be numbers to create datetime.date().
I'm still not seeing where is the unicode chance here.
comment:4 by , 17 years ago
the following alwayse asume "value" is string
33 def render(self, name, value, attrs=None):
34 try:
35 value = datetime.date(*map(int, value.split('-')))
But if there is no y, and m and d, will return datetime.date, if you try form_for_instance, then render it, you will see, just in my case, maybe I'm wrong
57 def value_from_datadict(self, data, files, name):
58 y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name)
59 if y and m and d:
60 return '%s-%s-%s' % (y, m, d)
61 return data.get(name, None)
comment:5 by , 17 years ago
Just to be clean here.
What "don't render correctly" means?. What did you see?
comment:6 by , 17 years ago
with this patch
IPython 0.8.1 -- An enhanced Interactive Python. ? -> Introduction to IPython's features. %magic -> Information about IPython's 'magic' % functions. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: from django.newforms import extras In [2]: from django import newforms In [3]: import datetime In [4]: w = extras.SelectDateWidget() In [5]: from BeautifulSoup import BeautifulSoup In [6]: BeautifulSoup(w.render('birthday',datetime.date.today())) Out[6]: <select name="birthday_month"> <option value="1">January</option> <option value="2">February</option> <option value="3">March</option> <option value="4">April</option> <option value="5">May</option> <option value="6">June</option> <option value="7">July</option> <option value="8">August</option> <option value="9">September</option> <option value="10">October</option> <option value="11">November</option> <option value="12" selected="selected">December</option> </select> <select name="birthday_day"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6" selected="selected">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> <option value="11">11</option> <option value="12">12</option> <option value="13">13</option> <option value="14">14</option> <option value="15">15</option> <option value="16">16</option> <option value="17">17</option> <option value="18">18</option> <option value="19">19</option> <option value="20">20</option> <option value="21">21</option> <option value="22">22</option> <option value="23">23</option> <option value="24">24</option> <option value="25">25</option> <option value="26">26</option> <option value="27">27</option> <option value="28">28</option> <option value="29">29</option> <option value="30">30</option> <option value="31">31</option> </select> <select name="birthday_year"> <option value="2007" selected="selected">2007</option> <option value="2008">2008</option> <option value="2009">2009</option> <option value="2010">2010</option> <option value="2011">2011</option> <option value="2012">2012</option> <option value="2013">2013</option> <option value="2014">2014</option> <option value="2015">2015</option> <option value="2016">2016</option> </select>
without this patch, this widget select nothing
comment:7 by , 17 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
You don't need to force unicode here, you should ask if the comming value is a String (unicode or str) or datetime.
(Duplicate of #5917?)
by , 17 years ago
Attachment: | SelectDate_datetime.3.patch added |
---|
This is IMHO a better way to do that (Err.. Sorry for the other one)
I don't see why you are forcing unicode to datetime values.