Django 自定义计数列表
我用这个代码生成一个记录列表,这些记录分为年份、品牌、系列、车身风格和车辆颜色。我想通过以下方式进一步定制:Django 自定义计数列表,django,django-queryset,Django,Django Queryset,我用这个代码生成一个记录列表,这些记录分为年份、品牌、系列、车身风格和车辆颜色。我想通过以下方式进一步定制: 在这一年里,我只想把2004年作为一个个体…剩下的将归入其他类别,即2009年、2008年、2007年、2006年、2005年、2004年、其他 对于make,我想显示六个受欢迎程度最高的make…在模型中有一个字段,我用来指定make的受欢迎程度,其值为primary(最高)、secondary或teeric。其余部分将归入其他类别 对于身体的风格和颜色,我想让少于3条记录的物品归入
- 在这一年里,我只想把2004年作为一个个体…剩下的将归入其他类别,即2009年、2008年、2007年、2006年、2005年、2004年、其他
- 对于make,我想显示六个受欢迎程度最高的make…在模型中有一个字段,我用来指定make的受欢迎程度,其值为primary(最高)、secondary或teeric。其余部分将归入其他类别
- 对于身体的风格和颜色,我想让少于3条记录的物品归入其他类别
year_count = vehicle_query.order_by(
'-common_vehicle__year__year').values('common_vehicle__year__year').
annotate(count=Count('id'))
make_count = vehicle_query.order_by(
'common_vehicle__series__model__manufacturer__manufacturer').
values('common_vehicle__series__model__manufacturer__manufacturer').
annotate(count=Count('id'))
style_count = vehicle_query.order_by(
'common_vehicle__body_style__style').values
('common_vehicle__body_style__style').annotate(count=Count('id'))
colour_count = vehicle_query.order_by(
'exterior_colour__exterior_colour').values(
'exterior_colour__exterior_colour').annotate(count=Count('id'))
您询问的大部分内容最好在Django之外处理,而不是由客户端javascript处理。明确地说,您可以让Django处理部分,但不这样做会更干净。这样做有很多好处:
标记(可能还有一些参数)时,该脚本将以您要求的格式呈现该列表
下面是一个使用jQuery的简单示例。对于本例,我将使用jQuery插件模式将功能包装在一个框架中
假设您的django模板输出以下内容
<ul>
<li>Chevy</li>
<li>Mazda</li>
<li>Honda</li>
<li>Ford</li>
<li>BMW</li>
</ul>
- 雪佛兰
- 马自达
- 本田
- 福特
- 宝马
jquery.showmorelist.js
(function($) {
$.fn.ShowMoreList = function(visibleItemCount) {
// Wrap parent element
var parent = $(this).wrap('<div class="show-more-list"></div>').parent();
var ul = $(this);
// Enumerate children and hide extras
var counter = 0;
$(this).children().filter('li').each(function(){
counter += 1;
if (counter > visibleItemCount) {
$(this).hide();
$(this).addClass('hidden');
}
});
// Add link and bind click
var link = $('<a href="#">> Show More</a>').click(function(){
$(ul).children().filter('.hidden').show();
});
$(parent).append(link);
}
})(jQuery);
(函数($){
$.fn.ShowMoreList=函数(visibleItemCount){
//换行父元素
var parent=$(this.wrap(“”).parent();
var ul=$(本);
//列举子项并隐藏额外项
var计数器=0;
$(this).children().filter('li').each(function()){
计数器+=1;
如果(计数器>visibleItemCount){
$(this.hide();
$(this.addClass('hidden');
}
});
//添加链接并单击“绑定”
var link=$('')。单击(函数(){
$(ul).children().filter('.hidden').show();
});
$(父项).附加(链接);
}
})(jQuery);
page.html
<script type="text/javascript" src="jquery.showmorelist.js"></script>
<script type="text/javascript">
// On page load...
$(function() {
$('ul').ShowMoreList(4); // Shows only the first 4 items
});
</script>
//页面加载时。。。
$(函数(){
$('ul').ShowMoreList(4);//仅显示前4项
});
这是一个相当简单的示例,它不会将“显示更多”切换为“隐藏更多”,但您应该能够从上下文中了解这一点。我设法找到了解决方案,因此我认为最好在此处更新答案: 在标题部分,我有以下内容:
<script type="text/javascript" src="{{ MEDIA_URL }}share/jquery/jquery.min.js"></SCRIPT>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
//hide the additional content under "Display More"
$("div.additional_content").hide();
$("a.more").click(function () {
//show or hide the additional content
$(this).siblings("div.additional_content").toggle();
//change the attributes and text value of the link toggle
if($(this).text() == "Display Less"){
$(this).removeClass("less");
$(this).addClass("more");
$(this).html("Display More");
}else{
$(this).removeClass("more");
$(this).addClass("less");
$(this).html("Display Less");
}
return false;
});
});
})(jQuery);
<div class="module_wrap">
<div class="module"> {% if year_count %} <strong>{% trans "Year" %}</strong> <br />
{% for item in year_count|slice:":6" %}
<ul>
<li> <a href="/inventory/year/{{ item.common_vehicle__year__year }}/">{{ item.common_vehicle__year__year }} ({{ item.count }})</a> {% if request.session.chosen_year %} <a href="/undo_year/"><img src="{{ MEDIA_URL }}img/undo.gif" border="0" alt="Remove Year Filter" title="Remove Year Filter" /></a> {% endif %} </li>
</ul>
{% endfor %}
<div class="additional_content"> {% for item in year_count|slice:"6:" %}
<ul>
<li> <a href="/inventory/year/{{ item.common_vehicle__year__year }}/">{{ item.common_vehicle__year__year }} ({{ item.count }})</a></li>
</ul>
{% endfor %} </div>
{% if year_count|slice:"6:" %}<a href="" class="more">Display More</a><br />
{% endif %} <br />
</div>
</div>
{% endif %}
(函数($){
$(文档).ready(函数(){
//隐藏“显示更多”下的其他内容
$(“div.additional_content”).hide();
$(“a.more”)。单击(函数(){
//显示或隐藏附加内容
$(this).sides(“div.additional_content”).toggle();
//更改链接切换的属性和文本值
如果($(this).text()==“显示较少”){
$(此).removeClass(“更少”);
$(此).addClass(“更多”);
$(this.html(“显示更多”);
}否则{
$(this.removeClass(“更多”);
$(此).addClass(“更少”);
$(this.html(“显示较少”);
}
返回false;
});
});
})(jQuery);
然后,无论我想减少可用选项的数量,我都有:
<script type="text/javascript" src="{{ MEDIA_URL }}share/jquery/jquery.min.js"></SCRIPT>
<script type="text/javascript">
(function($) {
$(document).ready(function() {
//hide the additional content under "Display More"
$("div.additional_content").hide();
$("a.more").click(function () {
//show or hide the additional content
$(this).siblings("div.additional_content").toggle();
//change the attributes and text value of the link toggle
if($(this).text() == "Display Less"){
$(this).removeClass("less");
$(this).addClass("more");
$(this).html("Display More");
}else{
$(this).removeClass("more");
$(this).addClass("less");
$(this).html("Display Less");
}
return false;
});
});
})(jQuery);
<div class="module_wrap">
<div class="module"> {% if year_count %} <strong>{% trans "Year" %}</strong> <br />
{% for item in year_count|slice:":6" %}
<ul>
<li> <a href="/inventory/year/{{ item.common_vehicle__year__year }}/">{{ item.common_vehicle__year__year }} ({{ item.count }})</a> {% if request.session.chosen_year %} <a href="/undo_year/"><img src="{{ MEDIA_URL }}img/undo.gif" border="0" alt="Remove Year Filter" title="Remove Year Filter" /></a> {% endif %} </li>
</ul>
{% endfor %}
<div class="additional_content"> {% for item in year_count|slice:"6:" %}
<ul>
<li> <a href="/inventory/year/{{ item.common_vehicle__year__year }}/">{{ item.common_vehicle__year__year }} ({{ item.count }})</a></li>
</ul>
{% endfor %} </div>
{% if year_count|slice:"6:" %}<a href="" class="more">Display More</a><br />
{% endif %} <br />
</div>
</div>
{% endif %}
{%if year\u count%}{%trans“year”}
{年度|计数|切片中的项目百分比::6”}
- {%if request.session.selected_year%}{%endif%}
{%endfor%}
{年度|计数中项目的百分比|切片:“6:%”
{%endfor%}
{%if year_count |切片:“6:%}
{%endif%}
{%endif%}
很抱歉,我不明白你的意思。我很难解释我的意思……我认为此链接显示得更好(年份、品牌、里程、车身风格、车身颜色的链接有一个“查看更多”部分,单击该部分可显示所有可用选项)谢谢…我正在寻找一个非javascript的解决方案,但由于我有一个最后期限,这将不得不做。我已经尝试了这段代码,但它不工作,因为我认为它会…它只显示2009年,然后所有其他的东西都会在“显示更多”…也没有发生什么事,当我点击“显示更多”