Could not parse the remainder: '{{' from '{{' , while inside html if loop - python

I am creating a table in html, but if the value that will be outputted is equal to the value above I want to merge the cells. I am sure that all the variables work as they are correct later. However, when I use them in the if loop I get an error
<table>
<tr>
<th>id</th>
<th>peptide_id</th>
<th>protein_id</th>
<th>group_id</th>
<th>search_id</th>
<th>peptide_parsimony</th>
</tr>
{% for elem in elem_list %}
<tr>
{% for sub_elem in elem %}
elem.2 =
{% if {{ elem.2 }} == {{sub_elem}} %}
<td> </td>
{% else %}
<td onclick="location.href='/protein/proteinseq/{{ elem.1}}/{{ elem.2 }}/{{ elem.4 }}/'" style = " text-decoration: underline; cursor: pointer" >{{ sub_elem }}</td>
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
This gives me the error :
Could not parse the remainder: '{{' from '{{'

Don't use tag or variable brackets when you are already within a tag.
{% if elem.2 == sub_elem %}

Related

multiple python lists to table rows in HTML

I have multiple lists created in python. I am trying to convert these lists into HTML rows.
week = ['1','2']
date = ['2022-10-01','2022-10-09']
My HTML table should look like below:
Week Date
1 2022-10-01
2 2022-10-09
what I tried so far:
{% for val in data %}
<tr>
<td>
{% for item in val.week %} {{ item }} <br> {% endfor %}
</td>
<td>
{% for item in val.date %} {{ item }} <br> {% endfor %}
</td>
</tr>
{% endfor %}
The problem with above code is every value is treated as cell instead of row and hence I am unable to apply any row related styles to the above.
Could someone help with how to convert these to rows instead of cells.
Thank you.
You can use dictionary comprehension to convert the two lists into a dictionary where the key would be the week number and the value would be the corresponding date. You can loop over the dictionary as key and value using the items attribute in the template.
# views.py
week = [1, 2]
date = ['2022-10-01','2022-10-09']
data = {week[i]: date[i] for i in range(len(week))}
return render(request, 'template.html', {'data': data})
# template.html
{% for key, value in data.items %}
<tr>
<td> {{ key }} </td>
<td> {{ value }} </td>
</tr>
{% endfor %}
try this approch...
------ views.py ------
week = ['1','2']
date = ['2022-10-01','2022-10-09']
data = tuple(zip(week,date))
------ html ------
<table style="border: 1px solid; border-collapse: collapse;">
<thead>
<th>Weeks</th>
<th>Dates</th>
</thead>
<tbody>
<tbody>
{% for i,j in data %}
<tr>
<td>{{i}}</td>
<td>{{j}}</td>
</tr>
{% endfor %}
</tbody>
</tbody>
</table>
-------- Output -------

Python list of dict to html table (Flask)

I have a list of dict:
data = [{'2479': {'2022-09-04 to 2022-09-10': 28, '2022-08-28 to 2022-09-03': 27},
'ADMINISTRATION': {'2022-09-04 to 2022-09-10': 8},
'1361': {'2022-09-04 to 2022-09-10': 4, '2022-08-28 to 2022-09-03': 5},
'PERSONAL TIME OFF': {'2022-08-28 to 2022-09-03': 8}}]
# '2479' is the project number
# '2022-09-04 to 2022-09-10' is the date range
# 28 is the total number of hours used for the project for that week
I'm trying to convert it to an html table like this:
So far, this is what I have:
Here's my code:
{% macro render_table_header(label) %}
{% for record in content["weeklyUserReport"] %}
{% for project in record %}
{% for week in record[project] %}
<th class="rotate-45 week1">
<div><span class="date">{{week}}</span></div>
</th>
{% endfor %}
{% endfor %}
{% endfor %}
{% endmacro %}
<table
class="table table-header-rotated table-striped-column hours-container"
id="grid"
>
<thead>
<tr>
<th class="row-header"></th>
{{ render_table_header() }}
</tr>
</thead>
<tbody>
{% for record in content["weeklyUserReport"] %}
{% for project in record %}
{% for week in record[project] %}
<tr>
{% if record[project] == project %}
<td class="project-name"></td>
{% else %}
<td class="project-name">{{ project }}</td>
{% endif %}
<td class="week1">{{ record[project][week] }}</td>
</tr>
{% endfor %}
{% endfor %}
{% endfor %}
</tbody>
</table>
I'd like duplicated projects to be removed and for the hours to line in to their corresponding rows. I have <td class="week2">, <td class="week3">, and <td class="week4"> for the other hours but I'm struggling to find a way to do that.
Any suggestions would be greatly appreciated! Thank you.
I think you were confused about how to properly do loops through the list of dictionaries on the jinja template. I can give a "replica" of what you have done. Try to do something like this on your HTML:
<body>
<table class="table table-header-rotated table-striped-column hours-container" id="grid" style="border: 1px solid black">
<thead>
<tr>
<th style="border: 1px solid black">project name</th>
{% for d in date %}
<th class="row-header" style="border: 1px solid black">{{d}}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for record in data %}
{% for project_key, project_value in record.items() %}
<tr>
<td>{{ project_key }}</td>
{% for value in project_value.values() %}
<td class="week1" style="border: 1px solid black">{{ value }}</td>
{% endfor %}
</tr>
{% endfor %}
{% endfor %}
</tbody>
</table>
</body>
Here is my controller:
#app.route("/")
def index():
data = [{'2479': {'2022-09-04 to 2022-09-10': 28, '2022-08-28 to 2022-09-03': 27},
'ADMINISTRATION': {'2022-09-04 to 2022-09-10': 8},
'1361': {'2022-09-04 to 2022-09-10': 4, '2022-08-28 to 2022-09-03': 5},
'PERSONAL TIME OFF': {'2022-08-28 to 2022-09-03': 8
}}]
date = ['2022-09-04', '2022-08-28']
return render_template('index.html', date=date, data=data)
Here is the result I got:

How to properly unpack a nested dictionary in a django HTML template?

So I was able to figure out how to unpack a dictionary keys and values on to a HTML template, but I am a bit confused as to how to unpack if if a dictionary value is a QuerySet. For example I passing in all the timeslots of the given user into the dictonary. How can I unpack the attributes of the TimeSlot QuerySet for each timeslot such as the start time and end time?
This is my HTML Template:
<table>
{% for key, value in user_info.items %}
{% for key2,value2 in value.items %}
<tr>
<td>{{key2}}</td>
<td>{{value2}}<td>
</tr>
{% endfor %}
<br>
{% endfor %}
</table>
My function in views.py
def check_food_availibility(request):
food = FoodAvail.objects.all()
timeslots = TimeSlot.objects.all()
users_dict = {}
for i in range(len(user_info)):
user = {
"author": None,
"food_available": None,
"description": None,
"timeslot": None
}
user["author"] = user_info[i].author.username
user["food_available"] = user_info[i].food_available
user["description"] = user_info[i].description
if TimeSlot.objects.filter(time_slot_owner=user_info[i].author):
user["timeslot"] = TimeSlot.objects.filter(time_slot_owner=user_info[i].author)
users_dict[user_info[i].author.username] = user
return render(request, "food_avail/view_food_avail.html", {"user_info": users_dict})
This is how it shows up currently:
try this
<table>
{% for key, value in user_info.items %}
{% for key2,value2 in value.items %}
<tr>
<td>{{key2}}</td>
{% if key2 == 'timeslot' %}
<td>
{% for i in value2 %}
i.start_time <-> i.end_time // just an example put your field here
{% endfor %}
</td>
{% else %}
<td>{{value2}}<td>
{% endif %}
</tr>
{% endfor %}
<br>
{% endfor %}
</table>

How to check if that which is iterated upon is the last key/value in a dict? (probably needs to be done in django template)

The desired outcome is a navigation that look like so:
A | B | C | D | E | ... | X | Y | Z
Note that both A and Z do not have the pipe on the outsides.
This is the template I have currently.
<section id="partners_nav">
<div class="row">
<table align="center" cellpadding="4px">
<tr>
{% for key, value in index.items %}
{% if key in index|last %}
{% if value == None %}
<td>{{ key }}</td>
<td id="partners_nav_bracket">|</td>
{% else %}
<td>{{ key }}</td>
<td id="partners_nav_bracket">|</td>
{% endif %}
{% else %}
{% if value == None %}
<td>{{ key }}</td>
<td id="partners_nav_bracket">|</td>
{% else %}
<td>{{ key }}</td>
<td id="partners_nav_bracket">|</td>
{% endif %}
{% endif %}
{% endfor %}
</tr>
</table>
</div>
The line {% if key in index|last %} is where i'm attempting to check if it is the last item in the iteration.
This does not produce any errors.Yet does not remove the pipe to the right of the letter Z.
Note:
Inside of index is an ordered dictionary which has a key for every letter in the alphabet. And some of these keys have values that are also a letter (the same letter)... This is to use the A | B | C as a jump to navigation at the top of the page. The rest of the keys have a value of None... So the letter still displays at the top of the page but it is not clickable
You might want to check forloop.last template variable. It returns True if it is the last time through the loop:
{% for key, value in index.items %}
...
{% if value %}
<td>{{ key }}</td>
{% else %}
<td>{{ key }}</td>
{% endif %}
{% if not forloop.last %}
<td id="partners_nav_bracket">|</td>
{% endif %}
...
{% endfor %}
You can make use of forloop.first or forloop.last.
<div class="row">
<table align="center" cellpadding="4px">
<tr>
{% for key, value in index.items %}
{% if value == None %}
<td>{{ key }}</td>
{% else %}
<td>{{ key }}</td>
{% endif %}
{% if not forloop.last %}
<td id="partners_nav_bracket">|</td>
{% endif %}
{% endfor %}
</tr>
</table>
</div>
I also cleaned up your markup, because the contents of your if and else were the same.

How to show sorted list by overall completion in Pootle

Hi I am new to Django and working on Pootle project.
I would like to sort by Overall completion by default in the pootle index page.
for example, http://pootle.locamotion.org/ lists the languages and sorted by names. you can click sort buttons to see sort by completion. but I would like to show this list sorted by completion whenever load a page.
in pootle/local_apps/pootle_app/templates/index/index.html,
<table class="sortable">
<tr>
<th>{% trans 'Language' %}</th>
<th>{% trans 'Overall Completion' %}</th>
<th>{% trans 'Last Activity' %}</th>
</tr>
{% for item in languages %}
{% ifnotequal item.total 0 %}
<tr class="{% cycle 'even' 'odd' %}">
<td class="language">
{{ item.name }}</td>
<td>
<div class="sortkey">{{ item.transper }}</div>
<div class="graph" title="{{ item.completed_title }}" dir="{% if LANGUAGE_BIDI %}rtl{% else %}ltr{% endif %}">
<div class="translated" style="width: {{ item.transper }}px"></div>
{% if item.fuzzy %}
<div class="fuzzy" style="{%if LANGUAGE_BIDI%}right{%else%}left{%endif%}: {{ item.transper }}px; width: {{ item.fuzzyper }}px"></div>
{% endif %}
{% if item.untrans %}
<div class="untranslated" style="{% if LANGUAGE_BIDI %}right{% else %}left{% endif %}: {{ item.transper|add:item.fuzzyper }}px; width: {{ item.untransper }}px"></div>
{% endif %}
</div>
</td>
<td>{{ item.lastactivity }}</td>
</tr>
{% endifnotequal %}
{% endfor %}
</table>
item.transper is the key that I want to sort by.
and this is how item and language is defined:
def get_items(request, model, get_last_action, name_func):
items = []
if not check_permission('view', request):
return items
for item in model.objects.iterator():
stats = item.getquickstats()
stats = add_percentages(stats)
lastact = get_last_action(item)
items.append({
'code': item.code,
'name': name_func(item.fullname),
'lastactivity': lastact,
'trans': stats["translatedsourcewords"],
'fuzzy': stats["fuzzysourcewords"],
'untrans': stats["untranslatedsourcewords"],
'total': stats["totalsourcewords"],
'transper': stats["translatedpercentage"],
'fuzzyper': stats["fuzzypercentage"],
'untransper': stats["untranslatedpercentage"],
'completed_title': _("%(percentage)d%% complete",
{'percentage': stats['translatedpercentage']}),
})
items.sort(lambda x, y: locale.strcoll(x['name'], y['name']))
return items
def getlanguages(request):
def get_last_action(item):
try:
return Submission.objects.filter(translation_project__language=item).latest()
except Submission.DoesNotExist:
return ''
return get_items(request, Language, get_last_action, tr_lang)
what should I change this so that I see sort by Overall Completion by default?
thank you so much for any suggestions
Change
items.sort(lambda x, y: locale.strcoll(x['name'], y['name']))
to
items.sort(lambda x, y: cmp(x['transper'], y['transper']))
(This works well with the builtin cmp function because the transper fields are converted to ints by the nice_percentage function, which is called as part of add_percentages)
If that doesn't produce the order you wanted, simply switch objects x and y.
If instead you need to sort it in the template (it's typically a bad idea to mess with third-party apps' source code), you can use the dictsort filter:
{% for item in languages|dictsort:"transper" %}
Again, if that's not the order you wanted, use dictsortreversed.
The latest development version of Pootle, to become Pootle 2.2, now remembers which column you have selected to sort on. So a page reload will now remember the order you last used.

Categories

Resources