【发布时间】:2016-05-21 09:52:51
【问题描述】:
在views.py中:
all_pages = 5
在 html 文件中:
{% if all_pages > 2 %}
<a href='...'>next</a>|<a href='...'>prev</a>
{% endif %}
{% if all_pages = page %}
<a href='...'>prev</a>
{% endif %}
但是当我在第 5 页时,仍然会出现两个 <a> 标签。
为什么第二个if 块不起作用?
我该如何解决?
============ 更新我的问题 =========
在views.py中:
def main(request):
list = PMenu.objects.all()
kol = request.META['PATH_INFO']
kol = kol[6:]
mylist = kol.split('-')
os = mylist[0]
sh = mylist[1]
en_chest_name = mylist[2]
cc = mylist[3]
page = mylist[4]
next_page = int(page) + 1
prev_page = int(page) - 1
senf = PDivContent.objects.get(id=cc)
#########################################################
users = PUser.objects.filter(ostan=os, shahr=sh, content_id=187)
#########################################################
all_users = 20
all_pages = math.ceil(all_users/4)
one = type(all_pages)
two = type(page)
#########################################################
And return part goes here...
在 main.html 中:
current page: {{ page }}<br>
all users: {{ all_users }}<br>
all pages: {{ all_pages }}<br>
content_id: {{ cc }}<br>
next page: {{ next_page }}<br>
type of all_pages: {{ one }}<br> #output => 0
type of page: {{ two }} #output => was empty
<hr>
{% if all_pages == 1 %}
there is only one page
{% elif all_pages > 1 and page == '1' %}
<a href='/main/{{ os }}-{{ sh }}-{{ en_chest_name }}-{{ cc }}-{{ next_page }}'>next</a>
{% elif all_pages > 1 and page == all_pages %}
this is the last page
{% else %}
<a href='/main/{{ os }}-{{ sh }}-{{ en_chest_name }}-{{ cc }}-{{ next_page }}'>next</a>|<a href='/main/{{ os }}-{{ sh }}-{{ en_chest_name }}-{{ cc }}-{{ prev_page }}'>prev</a>
{% endif %}
【问题讨论】:
-
我猜你有语法问题,如果喜欢,请更改第二个:
{% if all_pages == page %} -
我试过了,但是没有用。
-
语法应该是这样的。
views.py也可能存在一些问题。所以你可能想把它包括在你的问题中。 -
嗯,views.py 中的函数很长,但我认为问题出在其他地方。不能使用这个
{% if all_pages == page %},如果条件不能比较 django 模板中的 2 个变量。而且{% if page == all_pages %}也不起作用。 -
page似乎是一个字符串,而不是int而all_pages是 int。所以你不能比较它们。另外我建议将您的变量作为 url 参数传递,而不是您当前的方法。它会更干净,更快。
标签: django templates if-statement