| 239 | >>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'), required=False) |
| 240 | >>> print w.render('mydate', '') |
| 241 | <select name="mydate_month" id="id_mydate_month"> |
| 242 | <option value="-1">---</option> |
| 243 | <option value="1">Jan.</option> |
| 244 | <option value="2">Feb.</option> |
| 245 | <option value="3">March</option> |
| 246 | <option value="4">April</option> |
| 247 | <option value="5">May</option> |
| 248 | <option value="6">June</option> |
| 249 | <option value="7">July</option> |
| 250 | <option value="8">Aug.</option> |
| 251 | <option value="9">Sept.</option> |
| 252 | <option value="10">Oct.</option> |
| 253 | <option value="11">Nov.</option> |
| 254 | <option value="12">Dec.</option> |
| 255 | </select> |
| 256 | <select name="mydate_day" id="id_mydate_day"> |
| 257 | <option value="-1">---</option> |
| 258 | <option value="1">1</option> |
| 259 | <option value="2">2</option> |
| 260 | <option value="3">3</option> |
| 261 | <option value="4">4</option> |
| 262 | <option value="5">5</option> |
| 263 | <option value="6">6</option> |
| 264 | <option value="7">7</option> |
| 265 | <option value="8">8</option> |
| 266 | <option value="9">9</option> |
| 267 | <option value="10">10</option> |
| 268 | <option value="11">11</option> |
| 269 | <option value="12">12</option> |
| 270 | <option value="13">13</option> |
| 271 | <option value="14">14</option> |
| 272 | <option value="15">15</option> |
| 273 | <option value="16">16</option> |
| 274 | <option value="17">17</option> |
| 275 | <option value="18">18</option> |
| 276 | <option value="19">19</option> |
| 277 | <option value="20">20</option> |
| 278 | <option value="21">21</option> |
| 279 | <option value="22">22</option> |
| 280 | <option value="23">23</option> |
| 281 | <option value="24">24</option> |
| 282 | <option value="25">25</option> |
| 283 | <option value="26">26</option> |
| 284 | <option value="27">27</option> |
| 285 | <option value="28">28</option> |
| 286 | <option value="29">29</option> |
| 287 | <option value="30">30</option> |
| 288 | <option value="31">31</option> |
| 289 | </select> |
| 290 | <select name="mydate_year" id="id_mydate_year"> |
| 291 | <option value="-1">---</option> |
| 292 | <option value="2007">2007</option> |
| 293 | <option value="2008">2008</option> |
| 294 | <option value="2009">2009</option> |
| 295 | <option value="2010">2010</option> |
| 296 | <option value="2011">2011</option> |
| 297 | <option value="2012">2012</option> |
| 298 | <option value="2013">2013</option> |
| 299 | <option value="2014">2014</option> |
| 300 | <option value="2015">2015</option> |
| 301 | <option value="2016">2016</option> |
| 302 | </select> |
| 303 | >>> print w.render('mydate', '2010-04-15') |
| 304 | <select name="mydate_month" id="id_mydate_month"> |
| 305 | <option value="-1">---</option> |
| 306 | <option value="1">Jan.</option> |
| 307 | <option value="2">Feb.</option> |
| 308 | <option value="3">March</option> |
| 309 | <option value="4" selected="selected">April</option> |
| 310 | <option value="5">May</option> |
| 311 | <option value="6">June</option> |
| 312 | <option value="7">July</option> |
| 313 | <option value="8">Aug.</option> |
| 314 | <option value="9">Sept.</option> |
| 315 | <option value="10">Oct.</option> |
| 316 | <option value="11">Nov.</option> |
| 317 | <option value="12">Dec.</option> |
| 318 | </select> |
| 319 | <select name="mydate_day" id="id_mydate_day"> |
| 320 | <option value="-1">---</option> |
| 321 | <option value="1">1</option> |
| 322 | <option value="2">2</option> |
| 323 | <option value="3">3</option> |
| 324 | <option value="4">4</option> |
| 325 | <option value="5">5</option> |
| 326 | <option value="6">6</option> |
| 327 | <option value="7">7</option> |
| 328 | <option value="8">8</option> |
| 329 | <option value="9">9</option> |
| 330 | <option value="10">10</option> |
| 331 | <option value="11">11</option> |
| 332 | <option value="12">12</option> |
| 333 | <option value="13">13</option> |
| 334 | <option value="14">14</option> |
| 335 | <option value="15" selected="selected">15</option> |
| 336 | <option value="16">16</option> |
| 337 | <option value="17">17</option> |
| 338 | <option value="18">18</option> |
| 339 | <option value="19">19</option> |
| 340 | <option value="20">20</option> |
| 341 | <option value="21">21</option> |
| 342 | <option value="22">22</option> |
| 343 | <option value="23">23</option> |
| 344 | <option value="24">24</option> |
| 345 | <option value="25">25</option> |
| 346 | <option value="26">26</option> |
| 347 | <option value="27">27</option> |
| 348 | <option value="28">28</option> |
| 349 | <option value="29">29</option> |
| 350 | <option value="30">30</option> |
| 351 | <option value="31">31</option> |
| 352 | </select> |
| 353 | <select name="mydate_year" id="id_mydate_year"> |
| 354 | <option value="-1">---</option> |
| 355 | <option value="2007">2007</option> |
| 356 | <option value="2008">2008</option> |
| 357 | <option value="2009">2009</option> |
| 358 | <option value="2010" selected="selected">2010</option> |
| 359 | <option value="2011">2011</option> |
| 360 | <option value="2012">2012</option> |
| 361 | <option value="2013">2013</option> |
| 362 | <option value="2014">2014</option> |
| 363 | <option value="2015">2015</option> |
| 364 | <option value="2016">2016</option> |
| 365 | </select> |
| 367 | |
| 368 | # SelectTimeWidget ############################################################ |
| 369 | |
| 370 | >>> from django.forms.extras import SelectTimeWidget |
| 371 | >>> w = SelectTimeWidget() |
| 372 | >>> print w.render('mytime', '') |
| 373 | <select name="mytime_hour" id="id_mytime_hour"> |
| 374 | <option value="-1">---</option> |
| 375 | <option value="0">00</option> |
| 376 | <option value="1">01</option> |
| 377 | <option value="2">02</option> |
| 378 | <option value="3">03</option> |
| 379 | <option value="4">04</option> |
| 380 | <option value="5">05</option> |
| 381 | <option value="6">06</option> |
| 382 | <option value="7">07</option> |
| 383 | <option value="8">08</option> |
| 384 | <option value="9">09</option> |
| 385 | <option value="10">10</option> |
| 386 | <option value="11">11</option> |
| 387 | <option value="12">12</option> |
| 388 | <option value="13">13</option> |
| 389 | <option value="14">14</option> |
| 390 | <option value="15">15</option> |
| 391 | <option value="16">16</option> |
| 392 | <option value="17">17</option> |
| 393 | <option value="18">18</option> |
| 394 | <option value="19">19</option> |
| 395 | <option value="20">20</option> |
| 396 | <option value="21">21</option> |
| 397 | <option value="22">22</option> |
| 398 | <option value="23">23</option> |
| 399 | </select> |
| 400 | <select name="mytime_minute" id="id_mytime_minute"> |
| 401 | <option value="-1">---</option> |
| 402 | <option value="0">00</option> |
| 403 | <option value="1">01</option> |
| 404 | <option value="2">02</option> |
| 405 | <option value="3">03</option> |
| 406 | <option value="4">04</option> |
| 407 | <option value="5">05</option> |
| 408 | <option value="6">06</option> |
| 409 | <option value="7">07</option> |
| 410 | <option value="8">08</option> |
| 411 | <option value="9">09</option> |
| 412 | <option value="10">10</option> |
| 413 | <option value="11">11</option> |
| 414 | <option value="12">12</option> |
| 415 | <option value="13">13</option> |
| 416 | <option value="14">14</option> |
| 417 | <option value="15">15</option> |
| 418 | <option value="16">16</option> |
| 419 | <option value="17">17</option> |
| 420 | <option value="18">18</option> |
| 421 | <option value="19">19</option> |
| 422 | <option value="20">20</option> |
| 423 | <option value="21">21</option> |
| 424 | <option value="22">22</option> |
| 425 | <option value="23">23</option> |
| 426 | <option value="24">24</option> |
| 427 | <option value="25">25</option> |
| 428 | <option value="26">26</option> |
| 429 | <option value="27">27</option> |
| 430 | <option value="28">28</option> |
| 431 | <option value="29">29</option> |
| 432 | <option value="30">30</option> |
| 433 | <option value="31">31</option> |
| 434 | <option value="32">32</option> |
| 435 | <option value="33">33</option> |
| 436 | <option value="34">34</option> |
| 437 | <option value="35">35</option> |
| 438 | <option value="36">36</option> |
| 439 | <option value="37">37</option> |
| 440 | <option value="38">38</option> |
| 441 | <option value="39">39</option> |
| 442 | <option value="40">40</option> |
| 443 | <option value="41">41</option> |
| 444 | <option value="42">42</option> |
| 445 | <option value="43">43</option> |
| 446 | <option value="44">44</option> |
| 447 | <option value="45">45</option> |
| 448 | <option value="46">46</option> |
| 449 | <option value="47">47</option> |
| 450 | <option value="48">48</option> |
| 451 | <option value="49">49</option> |
| 452 | <option value="50">50</option> |
| 453 | <option value="51">51</option> |
| 454 | <option value="52">52</option> |
| 455 | <option value="53">53</option> |
| 456 | <option value="54">54</option> |
| 457 | <option value="55">55</option> |
| 458 | <option value="56">56</option> |
| 459 | <option value="57">57</option> |
| 460 | <option value="58">58</option> |
| 461 | <option value="59">59</option> |
| 462 | </select> |
| 463 | >>> w = SelectTimeWidget(required=False) |
| 464 | >>> print w.render('mytime', '15:45') |
| 465 | <select name="mytime_hour" id="id_mytime_hour"> |
| 466 | <option value="-1">---</option> |
| 467 | <option value="0">00</option> |
| 468 | <option value="1">01</option> |
| 469 | <option value="2">02</option> |
| 470 | <option value="3">03</option> |
| 471 | <option value="4">04</option> |
| 472 | <option value="5">05</option> |
| 473 | <option value="6">06</option> |
| 474 | <option value="7">07</option> |
| 475 | <option value="8">08</option> |
| 476 | <option value="9">09</option> |
| 477 | <option value="10">10</option> |
| 478 | <option value="11">11</option> |
| 479 | <option value="12">12</option> |
| 480 | <option value="13">13</option> |
| 481 | <option value="14">14</option> |
| 482 | <option value="15" selected="selected">15</option> |
| 483 | <option value="16">16</option> |
| 484 | <option value="17">17</option> |
| 485 | <option value="18">18</option> |
| 486 | <option value="19">19</option> |
| 487 | <option value="20">20</option> |
| 488 | <option value="21">21</option> |
| 489 | <option value="22">22</option> |
| 490 | <option value="23">23</option> |
| 491 | </select> |
| 492 | <select name="mytime_minute" id="id_mytime_minute"> |
| 493 | <option value="-1">---</option> |
| 494 | <option value="0">00</option> |
| 495 | <option value="1">01</option> |
| 496 | <option value="2">02</option> |
| 497 | <option value="3">03</option> |
| 498 | <option value="4">04</option> |
| 499 | <option value="5">05</option> |
| 500 | <option value="6">06</option> |
| 501 | <option value="7">07</option> |
| 502 | <option value="8">08</option> |
| 503 | <option value="9">09</option> |
| 504 | <option value="10">10</option> |
| 505 | <option value="11">11</option> |
| 506 | <option value="12">12</option> |
| 507 | <option value="13">13</option> |
| 508 | <option value="14">14</option> |
| 509 | <option value="15">15</option> |
| 510 | <option value="16">16</option> |
| 511 | <option value="17">17</option> |
| 512 | <option value="18">18</option> |
| 513 | <option value="19">19</option> |
| 514 | <option value="20">20</option> |
| 515 | <option value="21">21</option> |
| 516 | <option value="22">22</option> |
| 517 | <option value="23">23</option> |
| 518 | <option value="24">24</option> |
| 519 | <option value="25">25</option> |
| 520 | <option value="26">26</option> |
| 521 | <option value="27">27</option> |
| 522 | <option value="28">28</option> |
| 523 | <option value="29">29</option> |
| 524 | <option value="30">30</option> |
| 525 | <option value="31">31</option> |
| 526 | <option value="32">32</option> |
| 527 | <option value="33">33</option> |
| 528 | <option value="34">34</option> |
| 529 | <option value="35">35</option> |
| 530 | <option value="36">36</option> |
| 531 | <option value="37">37</option> |
| 532 | <option value="38">38</option> |
| 533 | <option value="39">39</option> |
| 534 | <option value="40">40</option> |
| 535 | <option value="41">41</option> |
| 536 | <option value="42">42</option> |
| 537 | <option value="43">43</option> |
| 538 | <option value="44">44</option> |
| 539 | <option value="45" selected="selected">45</option> |
| 540 | <option value="46">46</option> |
| 541 | <option value="47">47</option> |
| 542 | <option value="48">48</option> |
| 543 | <option value="49">49</option> |
| 544 | <option value="50">50</option> |
| 545 | <option value="51">51</option> |
| 546 | <option value="52">52</option> |
| 547 | <option value="53">53</option> |
| 548 | <option value="54">54</option> |
| 549 | <option value="55">55</option> |
| 550 | <option value="56">56</option> |
| 551 | <option value="57">57</option> |
| 552 | <option value="58">58</option> |
| 553 | <option value="59">59</option> |
| 554 | </select> |
| 555 | >>> w = SelectTimeWidget(required=True) |
| 556 | >>> print w.render('mytime', '15:45') |
| 557 | <select name="mytime_hour" id="id_mytime_hour"> |
| 558 | <option value="0">00</option> |
| 559 | <option value="1">01</option> |
| 560 | <option value="2">02</option> |
| 561 | <option value="3">03</option> |
| 562 | <option value="4">04</option> |
| 563 | <option value="5">05</option> |
| 564 | <option value="6">06</option> |
| 565 | <option value="7">07</option> |
| 566 | <option value="8">08</option> |
| 567 | <option value="9">09</option> |
| 568 | <option value="10">10</option> |
| 569 | <option value="11">11</option> |
| 570 | <option value="12">12</option> |
| 571 | <option value="13">13</option> |
| 572 | <option value="14">14</option> |
| 573 | <option value="15" selected="selected">15</option> |
| 574 | <option value="16">16</option> |
| 575 | <option value="17">17</option> |
| 576 | <option value="18">18</option> |
| 577 | <option value="19">19</option> |
| 578 | <option value="20">20</option> |
| 579 | <option value="21">21</option> |
| 580 | <option value="22">22</option> |
| 581 | <option value="23">23</option> |
| 582 | </select> |
| 583 | <select name="mytime_minute" id="id_mytime_minute"> |
| 584 | <option value="0">00</option> |
| 585 | <option value="1">01</option> |
| 586 | <option value="2">02</option> |
| 587 | <option value="3">03</option> |
| 588 | <option value="4">04</option> |
| 589 | <option value="5">05</option> |
| 590 | <option value="6">06</option> |
| 591 | <option value="7">07</option> |
| 592 | <option value="8">08</option> |
| 593 | <option value="9">09</option> |
| 594 | <option value="10">10</option> |
| 595 | <option value="11">11</option> |
| 596 | <option value="12">12</option> |
| 597 | <option value="13">13</option> |
| 598 | <option value="14">14</option> |
| 599 | <option value="15">15</option> |
| 600 | <option value="16">16</option> |
| 601 | <option value="17">17</option> |
| 602 | <option value="18">18</option> |
| 603 | <option value="19">19</option> |
| 604 | <option value="20">20</option> |
| 605 | <option value="21">21</option> |
| 606 | <option value="22">22</option> |
| 607 | <option value="23">23</option> |
| 608 | <option value="24">24</option> |
| 609 | <option value="25">25</option> |
| 610 | <option value="26">26</option> |
| 611 | <option value="27">27</option> |
| 612 | <option value="28">28</option> |
| 613 | <option value="29">29</option> |
| 614 | <option value="30">30</option> |
| 615 | <option value="31">31</option> |
| 616 | <option value="32">32</option> |
| 617 | <option value="33">33</option> |
| 618 | <option value="34">34</option> |
| 619 | <option value="35">35</option> |
| 620 | <option value="36">36</option> |
| 621 | <option value="37">37</option> |
| 622 | <option value="38">38</option> |
| 623 | <option value="39">39</option> |
| 624 | <option value="40">40</option> |
| 625 | <option value="41">41</option> |
| 626 | <option value="42">42</option> |
| 627 | <option value="43">43</option> |
| 628 | <option value="44">44</option> |
| 629 | <option value="45" selected="selected">45</option> |
| 630 | <option value="46">46</option> |
| 631 | <option value="47">47</option> |
| 632 | <option value="48">48</option> |
| 633 | <option value="49">49</option> |
| 634 | <option value="50">50</option> |
| 635 | <option value="51">51</option> |
| 636 | <option value="52">52</option> |
| 637 | <option value="53">53</option> |
| 638 | <option value="54">54</option> |
| 639 | <option value="55">55</option> |
| 640 | <option value="56">56</option> |
| 641 | <option value="57">57</option> |
| 642 | <option value="58">58</option> |
| 643 | <option value="59">59</option> |
| 644 | </select> |
| 645 | |
| 646 | Accepts a datetime or a string: |
| 647 | |
| 648 | >>> w.render('mydate', datetime.time(15, 45, 15)) == w.render('mydate', '15:45:15') |
| 649 | True |
| 650 | |
| 651 | Using a SelectDateWidget in a form: |
| 652 | |
| 653 | >>> class GetTime(Form): |
| 654 | ... mytime = TimeField(widget=SelectTimeWidget) |
| 655 | >>> a = GetTime({'mytime_hour':'15', 'mytime_minute':'45', 'mytime_second':'15'}) |
| 656 | >>> print a.is_valid() |
| 657 | True |
| 658 | >>> print a.cleaned_data['mytime'] |
| 659 | 15:45:15 |
| 660 | |
| 661 | As with any widget that implements get_value_from_datadict, |
| 662 | we must be prepared to accept the input from the "as_hidden" |
| 663 | rendering as well. |
| 664 | |
| 665 | >>> print a['mytime'].as_hidden() |
| 666 | <input type="hidden" name="mytime" value="15:45:15" id="id_mytime" /> |
| 667 | >>> b=GetTime({'mytime':'15:45:15'}) |
| 668 | >>> print b.is_valid() |
| 669 | True |
| 670 | >>> print b.cleaned_data['mytime'] |
| 671 | 15:45:15 |
| 672 | |
| 673 | |