【发布时间】:2018-07-09 22:14:11
【问题描述】:
我把我的代码文件放在这里,这样你就可以了解我的代码
在我的 index.html 中
{% load tag %}
<body>
<select name="select_year">
<option>2017</option>
<option>2018</option>
<option>2019</option>
<option>2020</option>
</select>
</br>
</br>
<table border="1">
<tr>
<td>No</td>
<td>Name</td>
<td>DOB</td>
<td>Year</td>
</tr>
{% for Manvi_User in ordering %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{ Manvi_User.first_name }}</td>
<td>{{ Manvi_User.dob}}</td>
<td>{{year|calculate_year:forloop.counter0}}</td>
</tr>
{% endfor %}
</table>
</body>
在 vies.py 中
def index(request):
all_user = Manvi_User.objects.all()
ordering = all_user.order_by('dob')
return render(request, 'index.html', {'ordering': ordering})
在 tag.py 中
@register.filter
def calculate_year(year, loop_counter):
try:
year = int(2017)
except ValueError:
return None
else:
return str(year + (loop_counter // 2))
在一年中,我使用 tag.py 放置静态年份
如果用户选择 2019,我只想显示具有 2019 年的数据
【问题讨论】:
标签: django python-3.x django-templates