On Mon, 12 Feb 2007 15:32:07 +1100, Malcolm Tredinnick wrote:

> On Mon, 2007-02-12 at 03:49 +0000, Patrick J. Anderson wrote:
>> I think I might be missing something, but I have a problem with
>> date filter. Why doesn't this template code work?
>> 
>> {% ifequal d|date:"l" "Sunday" %}
>>      <td class="sunday">
>> {% else %}
>>      <td>
>> {% endifequal %}
>> 
>> <ul><li>{{ d|date:"l" }}</li></ul>
>> 
>> But inside the HTML list item {{ d|date:"l" }} produces full weekday
>> names. 
> 
> The Django resolver for template tags is a bit simple-minded about what
> you can pass as arguments: basically either string literals or
> references to variables in the template's context are permitted. What it
> cannot do is taken something that is a context variable modified by a
> filter and work out that value. So Django is failing quietly on the
> first argument there and effectively comparing the empty string to the
> string "Sunday", I suspect.
> 
>> Can and should this evaluation be done in such a way, or perhaps
>> there's a better way or no way this can be done using Django templates?
> 
> Assuming this is related to your post from a few hours ago about
> determining if it is the first day of the week, I cannot really think of
> any way to do it with the default filters and template tags. A couple of
> possibilities that spring to mind:
> 
> (1) If your object had a "is first day of the week" method, you could
> call that. I realise in this case you are probably passing in standard
> Python datetime objects, in which case you are stuck, but maybe "d" is
> something custom that you have control over and create a method that
> returns a boolean.
> 
> (2) Write a custom template tag that tests if the date is the first day
> of the week (or equal to another date). It isn't too hard to write
> custom tags and it really is the recommended way for any kind of
> advanced functionality you want to make accessible through the template.
> You could pass the "d" variable and the day of the week to the tag and
> have it act like an if tag if the args were equal (copying the default
> "if" tag from django/templates/defaulttags.py as a starting point is one
> way to start here).
> 
> Regards,
> Malcolm
> 
> 
> 
> 
Thanks, Malcolm. I see. I didn't know that a Django tag couldn't take a
variable modified by a filter. Thanks for explaining that part. I'll try
to come up with a custom template tag by looking at the docs and Django
built-in tags in the file you mentioned.

Yes the posts were related, as I'm trying to plot an HTML 28-day calendar
with objects coming from a queryset. It's a little bit challenging with
Python and Django templating, but I'm determined to find a good solution
without resorting to PHP-style mixing of programming logic with
presentation.

Right now, I can easily create that calendar with dates using a custom tag
I write, but the trick is to properly associate events coming from my
model with the dates coming from context in my template tag.

Below is the code I wrote so far (inclusion tag and corresponding calendar
template):

[file:dashboardcalendar.py]

from datetime import datetime, timedelta
from django.template import Library,Node

register = Library()


def plot_calendar(context):
    """
    Plot milestones into an XHTML calendar
    
    Calendar starts today and ends at the end of day range defined by days 
(default = 28)
    """
    
    week = [datetime.today() + timedelta(days = n) for n in range(7)]
    week_days = [d.strftime("%A") for d in week]
    
    if context.has_key('calendar_milestones'):
        milestones = context['calendar_milestones']
    else:
        milestones = None

    if not context.has_key('weeks'):
        days = int(context['weeks']) * 7
    else:
        days = 28
        
    dates = []
    
    for n in range(days):
        date = datetime.today() + timedelta(days=n)
        dates.append(date)
   
    return {
        'week_days': week_days,
        'calendar_days': dates,
        'calendar_milestones': milestones,
    }

register.inclusion_tag('dashboard/widgets/calendar.html', 
   takes_context = True)(plot_calendar)



[file: calendar.html]
{% if week_days %}
<!-- BEGIN upcoming milestones claendar -->
<table summary="Milestones due in the next 4 weeks" cellspacing='1' 
class="calendar">
    <caption>Milestones due in the next 4 weeks</caption>
    <thead>
        <tr>
        {% for w in week_days %}
            <th>{{ w }}</th>
        {% endfor %}               
        </tr>
    </thead>
    
    <tbody>
    {% if calendar_days %}
        {% for d in calendar_days %}
                {% if forloop.counter0|divisibleby:7 %}
                        {% if not forloop.first %}</tr>{% endif %}
                        {% if not forloop.last %}<tr>{% endif %}
                {% endif %}
                {% if forloop.first %}
                <td class="today">
                        <ul><li><strong>TODAY</strong>
                        <!-- if there are milestones due today, show them here 
-->
                                
                {% else %}
                <td>
                        <ul><li>{{ d|date:"j N" }}
                {% endif %}
                        !-- if there are milestones due today, show them here 
-->
                                
                        </li></ul>
                </td>
        {% endfor %}
    {% else %}
        <tr>
                <td colspan="7">
                        <div class="error">Can't create calendar cells because 
I don't know
                        the date range</div>
                </td>
        </tr>
    {% endif %}
    </tbody>
</table>
<!-- END upcoming milestones calendar --> {% endif %}

<!-- just testing -->
{% if calendar_milestones %}
        <ul>
        {% for m in calendar_milestones %}
                <li>{{ m.date_close|date:"j F" }} | {{ m.name }} ({{ 
m.project.name }})</li>
        {% endfor %}
        </ul>
{% endif %}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to