0

I am trying to make a Django template page that uses Chart.js. The idea is that it would have fields for the user to add 'From' and 'To' date range to pass to Django template data to fill the Chart.js.

In order for me to figure out how to do that I need to know how to compare dates in Django templates but I can't seem to figure it out, again the below is just a test to figure out how you would compare dates in a template, simplified so I can try the harder parts.

entriesFuel.0.day is equal to March 5, 2020 and is a Django form DateField. I have tried the below

{% if entriesFuel.0.day == "5 March 2020" %}
<h1>{{entriesFuel.0.day}}</h1>
{% else %}
<h1>{{entriesFuel.1.day}}</h1>
{% endif %}

{% if entriesFuel.0.day == "March 5, 2020" %}
<h1>{{entriesFuel.0.day}}</h1>
{% else %}
<h1>{{entriesFuel.1.day}}</h1>
{% endif %}

{% if entriesFuel.0.day == "2020-03-05" %}
<h1>{{entriesFuel.0.day}}</h1>
{% else %}
<h1>{{entriesFuel.1.day}}</h1>
{% endif %}

Is it possible to compare the data this way or am I barking up the wrong tree by doing it this way?

Thank you,

  • What type is `entriesFuel.0.day`? A datetime or something custom? – Exelian Mar 20 '20 at 09:16
  • day is a DateField in the models `day = models.DateField(auto_now_add = True)` – Kingbuttmunch Mar 20 '20 at 09:18
  • Hmm, and how many items are there in `entriesFuel`? Is it just the from and to date? And is there a reason to do this in the template instead of the view? – Exelian Mar 20 '20 at 09:25
  • the entries will keep going up as it is a form for users to add to the database. currently it stands at 10 entries for testing purposes. I am no expert in Django this is purely a self learning excerise, my thought process was that the Chart.js would be 'dynamic' in that you could change the Date ranges. I would have thought I could pass the data from the 'From' and 'To' date fields in Javascript, to the Chart.js. By doing this in the views I would have thought you would add the dates you want before you enter the page? – Kingbuttmunch Mar 20 '20 at 09:34
  • 1
    You are comparing a datetime object to a string. I guess you could try the `date` filter, as shown [here](https://stackoverflow.com/a/7749024/4134674) (including link to the docs). Something like `entriesFuel.0.day|date:"Ymd" == "20200305"` – shmee Mar 20 '20 at 09:43
  • The filters part is what I needed, I will research the docs on this to hopefully other areas that it could cover. you answered the question here Shmee happy to accept your answer if posted as an answer. – Kingbuttmunch Mar 20 '20 at 10:14

1 Answers1

0

shmee exampling the date filter led to the answer for this. in the above example it is fixed with:

{% if entriesFuel.0.day|date:"dmY" == "05032020" %}
<h1>{{entriesFuel.0.day}}</h1>
{% else %}
<h1>{{entriesFuel.1.day}}</h1>
{% endif %}

which led to me using:

    data:{
  labels:[{%for entryFuel in entriesFuel %}{%if entryFuel.day|date:"dmY" > "07032020" and entryFuel.day|date:"dmY" < "10032020"%}"{{entryFuel.day}}",
  {% endif %}
  {% endfor%}],
  datasets:[{
    label:'Stuff Added',
    data: [{%for entryFuel in entriesFuel %}{%if entryFuel.day|date:"dmy" > "07032020" and entryFuel.day|date:"dmY" < "10032020"%} "{{entryFuel.buckets_added}}",
    {% endif %}
    {% endfor %},
    ],

https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#date

  • 1
    It looks like you're trying to render JSON with templating. I would suggest you seriously reconsider this. Speaking from experience trying to render JSON is a lot of effort for something which could easily be handle by `json.dumps` and rendering the in the view. If you need JSON like this, perhaps consider the `json_script` template filter. This would safely render the data you wouldn't be reinventing the wheel – Exelian Mar 20 '20 at 11:05
  • I absolutely agree with @Exelian in the comment here and below the question. What you show here looks like it would better be done in the view and then passed as readily compiled dataset to the template for plain display. Also, formatting the date as day-month-year may have unwanted side effects, `01042020` is smaller than `29032020` in string comparison, but in date comparison, April 1st 2020 is certainly greater than March 29th 2020. If you _have_ to compare dates as strings, better use year-month-day with a four-digit year and month and day in two digits with leading zeroes. – shmee Mar 20 '20 at 12:39
  • What kind of examples or reading material would either of you suggest I look at to better complete this? I am trying to update a Chart.js based on a user date input and this was just the avenue I went down. This is purely just a learning exercise so I would rather learn the correct method. Thank you – Kingbuttmunch Mar 20 '20 at 13:13
  • It seems like you are trying to generate the JSON data to feed to Chart.js. If that really is the case, you might want to build your dataset in the view, as already suggested, where you have all of Python's tools available to manipulate the data as needed. Instead of rendering a template with the result, you could return a [`JsonResponse`](https://docs.djangoproject.com/en/3.0/ref/request-response/#jsonresponse-objects) that you read directly from your javascript code via an [AJAX](https://en.wikipedia.org/wiki/Ajax_(programming)) call. – shmee Mar 20 '20 at 19:49