From 940edfc23f1764a46e69b2e878d4b8b68e540d55 Mon Sep 17 00:00:00 2001
From: Marc Fargas <telenieko@telenieko.com>
Date: Mon, 15 Sep 2008 17:00:36 +0200
Subject: [PATCH] Rewrite of the variable resolution section, should go on the next 1.0
Fixed wrong cross-reference in QueryDict.lists() docs.
---
docs/howto/custom-template-tags.txt | 50 +++++++---------------------------
docs/ref/request-response.txt | 2 +-
2 files changed, 12 insertions(+), 40 deletions(-)
diff --git a/docs/howto/custom-template-tags.txt b/docs/howto/custom-template-tags.txt
index e1ddefe..80a3d45 100644
a
|
b
|
Now your tag should begin to look like this::
|
523 | 523 | raise template.TemplateSyntaxError, "%r tag's argument should be in quotes" % tag_name |
524 | 524 | return FormatTimeNode(date_to_be_formatted, format_string[1:-1]) |
525 | 525 | |
| 526 | .. versionchanged:: 1.0 |
| 527 | Variable resolution has changed in the 1.0 release of Django. ``template.resolve_variable()`` |
| 528 | has been deprecated in favor of a new ``template.Variable`` class. |
| 529 | |
526 | 530 | You also have to change the renderer to retrieve the actual contents of the |
527 | 531 | ``date_updated`` property of the ``blog_entry`` object. This can be |
528 | | accomplished by using the ``resolve_variable()`` function in |
529 | | ``django.template``. You pass ``resolve_variable()`` the variable name and the |
530 | | current context, available in the ``render`` method:: |
| 532 | accomplished by using the ``Variable()`` class in ``django.template``. |
| 533 | |
| 534 | To use the ``Variable`` class, simply instantiate it with the name of the |
| 535 | variable to be resolved, and then call ``variable.resolve(context)``. So, |
| 536 | for example:: |
531 | 537 | |
532 | | from django import template |
533 | | from django.template import resolve_variable |
534 | | import datetime |
535 | 538 | class FormatTimeNode(template.Node): |
536 | 539 | def __init__(self, date_to_be_formatted, format_string): |
537 | | self.date_to_be_formatted = date_to_be_formatted |
| 540 | self.date_to_be_formatted = Variable(date_to_be_formatted) |
538 | 541 | self.format_string = format_string |
539 | 542 | |
540 | 543 | def render(self, context): |
541 | 544 | try: |
542 | | actual_date = resolve_variable(self.date_to_be_formatted, context) |
| 545 | actual_date = self.date_to_be_formatted.resolve(context) |
543 | 546 | return actual_date.strftime(self.format_string) |
544 | 547 | except template.VariableDoesNotExist: |
545 | 548 | return '' |
546 | 549 | |
547 | | ``resolve_variable`` will try to resolve ``blog_entry.date_updated`` and then |
548 | | format it accordingly. |
549 | | |
550 | | .. versionadded:: 1.0 |
551 | | |
552 | | Variable resolution has changed in the development version of Django. |
553 | | ``template.resolve_variable()`` is still available, but has been deprecated |
554 | | in favor of a new ``template.Variable`` class. Using this class will usually |
555 | | be more efficient than calling ``template.resolve_variable`` |
556 | | |
557 | | To use the ``Variable`` class, simply instantiate it with the name of the |
558 | | variable to be resolved, and then call ``variable.resolve(context)``. So, |
559 | | in the development version, the above example would be more correctly |
560 | | written as: |
561 | | |
562 | | .. parsed-literal:: |
563 | | |
564 | | class FormatTimeNode(template.Node): |
565 | | def __init__(self, date_to_be_formatted, format_string): |
566 | | self.date_to_be_formatted = **Variable(date_to_be_formatted)** |
567 | | self.format_string = format_string |
568 | | |
569 | | def render(self, context): |
570 | | try: |
571 | | actual_date = **self.date_to_be_formatted.resolve(context)** |
572 | | return actual_date.strftime(self.format_string) |
573 | | except template.VariableDoesNotExist: |
574 | | return '' |
575 | | |
576 | | Changes are highlighted in bold. |
577 | | |
578 | 550 | Variable resolution will throw a ``VariableDoesNotExist`` exception if it cannot |
579 | 551 | resolve the string passed to it in the current context of the page. |
580 | 552 | |
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
index 89e5195..333a972 100644
a
|
b
|
In addition, ``QueryDict`` has the following methods:
|
348 | 348 | |
349 | 349 | .. method:: QueryDict.lists() |
350 | 350 | |
351 | | Like :method:items(), except it includes all values, as a list, for each |
| 351 | Like :meth:`items()`, except it includes all values, as a list, for each |
352 | 352 | member of the dictionary. For example:: |
353 | 353 | |
354 | 354 | >>> q = QueryDict('a=1&a=2&a=3') |