第二个if块在django模板文件中不起作用

时间:2016-05-21 09:52:51

标签: django templates if-statement

在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 %}

1 个答案:

答案 0 :(得分:1)

all_pages似乎是一个字符串类型,这里不是整数,而private class SqlParamDefinition { public SqlParamDefinition(string name, SqlDbType dbType, object value) { this.Name = name; this.DbType = dbType; this.Value = value; } public string Name { get; } public SqlDbType DbType { get; } public object Value { get; } } 是整数。所以你无法比较它们。 此外,我建议将您的变量作为url参数传递,而不是当前的方法,它将更清洁,更快。