views:

185

answers:

1

I'm not sure why, but this condition will never evaluate True for me. I'm feeding it datetime.today() in the urls file. Am I missing something?

Template:

{% load humanize %}

{{ entry.date|naturalday }}  {# Evals to "today" #}

{% ifequal entry.date|naturalday "today" %}
    True
    {{ entry.date|date:"fA"|lower }} {{ entry.date|naturalday|title }}
{% else %}
    False
    {{ entry.date|naturalday|title }}
{% endifequal %}
+1  A: 

I just tested this under Django 1.1.1 and it works just fine for me.

Which version of Django are you running?

However, there are a few other issues that could be causing you problems:

  1. I also noticed that in your question you have {% load humaize %}, which contains a typo (should be {% load humanize %}). I'm not sure if this is in your real code or just in your question though.

  2. If you are really using datetime.today() in your urls.py, as you say, please be aware that this could cause problems, since the value is only going to be calculated once, when the extra_context dictionary is first created (since the value of "today" will only ever be calculated once). This could mean the code will work on the first day the app is running, then fail the second day. You likely wouldn't notice this until you deploy to an environment where the app runs overnight without being restarted.

    If you want it to really be "today", just pass in the function datetime.today rather than datetime.today(). That way the template will call it on each render.

TM
Thank you for your response! 1. yes the 'humaize' was a typo in the question only. 2. When I pass the function without calling it, I get this error: Caught an exception while rendering: 'builtin_function_or_method' object has no attribute 'weekday'
Scott Willman
Are you passing `datetime.today` directly in as a template variable, or are you using it in some other way? Template variables can be actual values or callables, but if you have python code using the value as well, you'd need to adjust it to call it first before trying to access `weekday`. It'd help if you could show a bit more of how the date is being used.
TM