[英]Django, template attribute unique
I'm trying to implement a list of events to a js calendar. 我正在尝试实现js日历的事件列表。 So the data for the calendar are like this:
因此,日历的数据如下所示:
var studyEvents = {
'04-03-2014' : '<span class="event">Event 1</span>',
'05-08-2014' : '<span class="event">Event 2</span>',
'12-09-2014' : '<span class="event">Event 3</span>'
};
In case the are more than one events on the same day , the data are like this: 如果同一天有多个事件,则数据如下:
var studyEvents = {
'04-03-2014' : '<span class="event">Event 1</span>',
'05-08-2014' : '<span class="event">Event 2-1</span><span class="event">Event 2-2</span>',
'12-09-2014' : '<span class="event">Event 3</span>'
};
So what I'm doing is getting a list with all events ordered by date. 所以我正在做的是获取按日期排序的所有事件的列表。 I want to check if there are more than one events in on the same day .
我想检查同一天是否有多个事件。 I'm thinking that I could check(since the list is ordered by date) if the
event.date
is not unique then there could be a way to put it on the same line. 我想我可以检查(因为列表按日期排序)如果
event.date
不是唯一的,那么可以有一种方法将其放在同一行上。
var studyEvents = {
{% for event in events %}
'{{ event.date|date:"m-d-Y" }}' : '<span class="event">{{ event.title }}</span>'{% if not forloop.last %},{% endif %}
{% endfor %}
};
I'm a bit lost with this one so any help would be much appreciated! 我对这个感到有些迷惑,因此不胜感激!
You can use the built-in templatetag regroup
on date. 您可以按日期使用内置的templatetag
regroup
。 https://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup https://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup
Example usage: 用法示例:
cities = [
{'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'},
{'name': 'Calcutta', 'population': '15,000,000', 'country': 'India'},
{'name': 'New York', 'population': '20,000,000', 'country': 'USA'},
{'name': 'Chicago', 'population': '7,000,000', 'country': 'USA'},
{'name': 'Tokyo', 'population': '33,000,000', 'country': 'Japan'},
]
template: 模板:
{% regroup cities by country as country_list %}
<ul>
{% for country in country_list %}
<li>{{ country.grouper }}
<ul>
{% for item in country.list %}
<li>{{ item.name }}: {{ item.population }}</li>
{% endfor %}
</ul>
</li>
{% endfor %}
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.