Blocktrans correctly written in django template - django

I want my django template variables were possible to translate. I used for this purpose {% blocktrans %}.
Is this code is correct and can be optimized. I mean to write better? The loop or something like that.
{% for obj in objects %}
{% blocktrans with obj.user as user and obj.country as country and obj.b_day as b_day %}
<tr>
<td>{{ user }}</td>
<td>{{ country }}</td>
<td>{{ b_day }}</td>
</tr>
{% endblocktrans %}
{% endfor %}

Hm, I suspect your code don't work properly. Anyway the simple {% trans %} tag looks much better:
{% for obj in objects %}
<tr>
<td>{% trans obj.user %}</td>
<td>{% trans obj.country %}</td>
<td>{% trans obj.b_day %}</td>
</tr>
{% endfor %}

Related

how to get random 1/2 data from database in jinj2 template in Django?

<table>
{% for field in fields %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endfor %}
</table>
here we will get all the data from fields . but i want to only get randomly 1/2 (i can specified how many) data in jinja2 template from backend ?
How to do this ?
<table>
{% for field in fields %}
{% if forloop.counter < x %}
<tr>
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
just put your desired number in x,you are good to go.
Try this. You can add this logic to the frontend. This way it will display records with even ID. and those will 1/2 as well.
<table>
{% for field in fields %}
<tr>
{% if field.id%2 == 0 %}
<td>{{ field.name }}</td>
<td>{{ field.value }}</td>
{% endif %}
</tr>
{% endfor %}
</table>

Django Template IF inside Listing FOR

I am tryin use an object from a listing in an IF structure inside the FOR LOOP, but when I am trying to compare the object whit a String (That is 'TRUE'), I can not go inside the True case lines of the IF structure.
Example:
When equipo.Department = "Equipo", i don know why the IF ({% if equipo.Department == 'Equipo' %}) is not working.
Code:
{% autoescape off %}
{% if equipos %}
{% for equipo in equipos %}
<tr></tr>
<td>{% if equipo.Department == 'Equipo' %}
E
{% else %}{{ equipo.Department }}{% endif %}-{{ equipo.Equipment_id }}</td>
<td>{{ equipo.Name }}</td>
<td>{{ equipo.Description }}</td>
<td>{{ equipo.SerialNo }}</td>
<td>{{ equipo.Vendor }}</td>
<td>{{ equipo.Tag }}</td>
<td>{{ equipo.OutOfService }}</td>
<td>{{ equipo.Location }}</td>
<td>{{ equipo.Plan }}</td>
<td>{{ equipo.ManualPath }}</td>
<td>{{ equipo.ImagePath }}</td>
</tr>
{% endfor %}
{% else %}
<h1>No existen registros</h1>
{% endif %}
{% endautoescape %}
A Department object with 'Equipo' as name is not the same as the string 'Equipo', so the check itself is false. If you later render {{ equipo.Department }}, then Django will call the str(..) method on that result, and thus it will indeed render the name of the department.
You thus should check the equivalence with:
{% if equipo.Department.name == 'Equipo' %}
<!-- … -->
{% endif %}
That being said, instead of filtering in the template, you better should filter at the database end, so usually it is better to try to avoid retrieving equipo that is not of the required department.
You can filter for example with:
SomeModel.objects.filter(Department__name='Equipo')
Note: Django has a {% for … %}…{% empty %} template tag [Django-doc] that
can be used to render a message if the collection you iterate over is empty.

Django template - How to match model datefield with a date in date generator?

In my models.py Task can have multiple Employer so I use ManyToManyField
In my views.py I got a date generator - it return range of 2 weeks from current day.
So here is my template
<table>
<thead>
<tr>
<th>{% trans 'employer_id' %}</th>
{% for i in date_range %}
<th>{{ i }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for employer in employers %}
<tr>
<td>{{ employer.employer_id }}</td>
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
What I need is when I create a Task and assign it to employers, it should show me in the html table a start_date of Task of each employers for specific days.
So far I got this with codes above:
^date of tasks of these employers are not matching with days in the table header.
Instead of
{% for t in employer.task_set.all %}
<td>{{ t }}</td>
{% empty %}
<td>0</td>
{% endfor %}
you need to iterate over the dates again, since you're not creating <td>'s for each date.
{% for i in date_range %}
{% for t in employer.task_set.all %}
{% if t.start_date == i %}
<td>{{ t }}</td>
{% else %}
<td>-</td>
{% endif %}
{% empty %}
<td>0</td>
{% endfor %}
{% endfor %}

django template view nested list

i made a nested list and from that id like to make a table in my template
list looks something like
ground_skills_available = [[category1, [skill1, skill2, skill3]], [category1, [skill1, skill2]]]
Now i want to list it that you got category with below the items, next category and other items. Problem is, i have no clue about hot to show only category, instead of category +all items, since you cant seem to use the index of a list?
Can someone please help me out?
<table>
{% for categories in ground_skills_available %}
{% for category in categories %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
Change your outer loop to
{% for category, skills in ground_skills_available %}
This technique is described in the Django for loop docs.
On the first iteration of the loop, this will take the list [category1, [skill1, skill2, skill3], and assign
category = category1
skills = [skill1, skill2, skill3]
You can then display {{ category }}, and loop through skills.
Putting that together, you have:
<table>
{% for category, skills in ground_skills_available %}
<tr>
<td>{{ category }}</td>
</tr>
{% for skill in skills %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
{% if forloop.index == 1 %} ... some code ... {% endif %}
or
{% if forloop.index0 == 0 %} ... some code ... {% endif %}
Expectation:
{% for categories in ground_skills_available %}
{% for category in categories %}
{% if forloop.first %}
<tr>
<td>{{ category }}</td>
</tr>
{% else %}
{% for skill in category %}
<tr>
<td>{{ skill.name }}</td>
</tr>
{% endfor %}
{% endif %}
{% endfor %}
{% endfor %}

django form label in {% for %} statement

I want to wrap a in a label, but when the template was rendered, it didn't generate the right html, here is my code:
{% for item in studentinfo %}
<form action="" method="">
{% csrf_token %}
<tr>
<td>{{ item.nickname|default_if_none:"" }}</td>
<td>{{ item.mobile|default_if_none:"" }}</td>
<td>{{ item.register_at|date:"Y-m-d"|default_if_none:"" }}</td>
<td>{{ item.sales.first_name|default_if_none:"" }}</td>
<td><strong class="red-text">{{ item.price|default_if_none:"" }}</strong></td>
<td><strong class="red-text">{{ item.remaining|default_if_none:"" }}</strong></td>
<td>{{ item.level|default_if_none:"" }}</td>
<td>
{% if item.state %}
{{ setstudentform.state|default:item.state }}
{% else %}
{{ setstudentform.state }}
{% endif %}
</td>
<td>{{ item.source|default_if_none:"" }}</td>
<td>
{% if item.feature %}
{{ setstudentform.feature|default:item.feature }}
{% else %}
{{ setstudentform.feature }}
{% endif %}
</td>
</tr>
</form>
{% endfor %}
but it's generated html was like this:
<form method="" action=""></form>
<input type="hidden" value="8N1O4Oks4MmgN1ujanMZX0o2X5XGUMny" name="csrfmiddlewaretoken">
with nothing inside the <form>, why is that
You want this:
<form method="POST">
{% csrf_token %}
<table>
{% for item in studentinfo %}
<tr>
<td>...</td>
</tr>
{% endfor %}
</table>
</form>
The way you template is structured is not valid HTML.
Also, you don't need this:
{% if item.feature %}
{{ setstudentform.feature|default:item.feature }}
{% else %}
{{ setstudentform.feature }}
{% endif %}
Simply {{ setstudentform.feature|default:item.feature }} will do what you intend.
The question is incomplete, I think -- we'd need to see what's around that {% for %} too, but I'll hazard a guess.
It looks like you're trying to interleave forms between table rows, which isn't likely legal in HTML and will cause the parsed DOM tree to look possibly something like your "generated HTML" example.
Did you check the HTML on the wire (using View Source (aside from IE, which when I last used it always output the parsed DOM tree as "source"), not a DOM inspector)? My educated guess is you'll find your HTML correct (or well, at least as you've templated it :) ) there.
So, in short, I don't think
<table>
<form><tr>...</tr></form>
<form><tr>...</tr></form>
</table>
isn't legal HTML.