Hello everyone…
As mentioned… I am new to Django… hope to get some answers, most likely me doing something wrong.
Working on a simple site, where I get data from an API and then display a table with the results in a webpage. Here is my code:
{% for i in response %}
{% if i.type != "personal" %}
<tr>
<td>{{i.name}}</td>
<td>{{i.type}}</td>
<td>{{i.status}}</td>
{% CreatedDate = i.createdAt %}
{% CreatedDateFormated = CreatedDate[:10] %}
<td>{{i.createdAt}}</td>
<td>{{i.id}}</td>
<td>{{i.key}}</td>
{% url = "https://elkvalleyresources.atlassian.net/wiki" + i.key %}
<td><a href="{{ url }}">{{i.name}}</a></td>
</tr>
{% endif %}
{% endfor %}
The CreatedAt field, it supposed to be a string, and is coming in format 2024-05-21T15:51:55.353Z, so I wanted to just get the first 10 chars… so … I try this:
i.createdAt[:10]
Sadly, did not work… then I tried what you see in the code:
{% CreatedDate = i.createdAt %}
{% CreatedDateFormated = CreatedDate[:9] %}
but got error about me forgetting to TAG CreatedDateFormated.
Also… the href I just trying a simply string concatenation
url = "https://elkvalleyresources.atlassian.net/wiki" + i.key'
and then I pass that variable in the href, but I get this error, and do not really know what is it.
Any help will be appreciated
Thanks
NG.
Welcome @NormanGarciaV !
The Django template language is quite limited by design.
Instead of what you’re tried to do, you need to look at the builtin tags and filters for the functionality needed.
Working under the assumption that the i.createdAt
variable is a DateTimeField and not a string, you’re probably looking to use the date
filter.
Well, this is kinda correct. But in reality, it’s more accurately described as injecting a variable into your html.
What you’re more looking for here is something less complex and more straight-forward:
<td><a href="https://elkvalleyresources.atlassian.net/wiki/{{ i.key }}">{{i.name}}</a></td>
Hi Ken…
Thanks for the help… the HREF worked… but the still having issues with the date… is not throwing an error… but is simply not returning anything, the column is empty. Here is how I wrote it:
<td>{{i.createdAt | date:"d-M-Y"}}</td>
And shows nothing.
What am I doing wrong?
Thanks.
Norman.
We would need to see the model and the view to begin to diagnose that.
Side note, the parser for tags is very limited. There are places where extra spaces will cause problems. I’d try getting rid of those spaces in that line:
<td>{{ i.createdAt|date:"d-M-Y" }}</td>
(I don’t know for sure if this is one of those cases, but this is how it’s shown in the docs.)
Yes… I thought of that too after I replied to you… tried… and still shows blanks.
Do we need to, sort of, “Declared” the filter before using it?
In the documentation I see
{{ django|title}}
with the context of {‘django’: ‘the web framework for perfectionists with deadlines’}, this template renders to:
The Web Framework For Perfectionists With Deadlines
Do we?
We need to see the view, any related models, and a sample of the data being rendered.
Hi Ken…
Well you got me thinking… as it was returning empty… then I thought that was not a date field…
Checked the filters… found the truncate and it seems to work…
<td>{{i.createdAt|truncatechars:11}}</td>
now… just thinking… the truncate leaves those “…” (three dots) at the end…
is there a way to remove them?
Thans.
Try using {{i.createdAt|slice:11}}
instead.
But it seems odd that createdAt
is a string and not a datetime…
Thanks Philgyfor… slide worked great.
Well… when getting the response data from the API… I return the response.json(), which, I am assuming, is text in json format…
something puzzles me tho:
in the views.py file… when I was debugging the code, using visual studio code, I used the debug console and when evaluating the variable where I save the response, it was a dictionary, so to check the value of things… I used the variable[‘key’] convention…
But when the template gets the data… I had to use the object process… as obj_name.property.
So, does Django converts dicts into objects when pass to templates?
Nope. No conversion occurs, it’s how the template language parser works.
See the “Behind the scenes” note box at The Django template language | Django documentation | Django
Great… thanks a lot guys… much appreciated.