python Django 模板 ifequal 标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2099064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:46:42  来源:igfitidea点击:

Django template ifequal tag

pythondjangodjango-templates

提问by The.Anti.9

I'm using an ifequal tag in my django template inside a loop where atleast one of the items should equal the other at some point in the loop but for some reason it never displays what it should. I was wondering if there are any weird cases that i should know about.

我在循环内的 django 模板中使用 ifequal 标签,其中至少一个项目应该在循环中的某个点等于另一个项目,但由于某种原因,它永远不会显示它应该显示的内容。我想知道是否有任何我应该知道的奇怪案例。

I have a list of int city ID's that should be checked as check boxes. so as i loop through all of the cities, for each one i loop through the ones that are supposed to be checked to see if the equal anywhere in the list. But for whatever reason none of them ever match. I verified that the data is right using the django shell, so i know its there, i think i'm missing some small detail with how i'm using it. Heres the code:

我有一个 int city ID 的列表,应该作为复选框选中。因此,当我遍历所有城市时,对于每个城市,我都会遍历那些应该检查的城市,以查看列表中的任何地方是否相等。但无论出于何种原因,它们都不匹配。我使用 django shell 验证了数据是正确的,所以我知道它在那里,我想我缺少一些关于我如何使用它的小细节。代码如下:

View:

看法:

def editprof(request):
    try:
        if request.session['id']:
            loggedin = True
    except KeyError:
        loggedin = False
    try:
        citylist = CityList.objects.all()
        userid = request.session['id']
        user = MemberProfile.objects.get(pk=userid)
        p = decrypt_pwd(user.Password)
        pflags = user.PublicVisibleFlags
        log_val(pflags[0])
        pflags = pflags.split(',')
        mflags = user.MemberVisibleFlags
        log_val(mflags[0])
        mflags = mflags.split(',')
        return render_to_response('editprof.html', {'user':user, 'p':p, 'loggedin':loggedin, 'citylist':citylist, 'pflags':pflags, 'mflags':mflags})
    except KeyError:
        return HttpResponse('You must be logged in to view this page!')
    except MemberProfile.DoesNotExist:
        return HttpResponse('DatabaseError')

Template clip:

模板剪辑:

{% for city in citylist %}
 <tr>
  <td><input type='checkbox' name='public' value='{{ city.id }}' {% for id in pflags %}{% ifequal id city.id %}checked{% endifequal %}{% endfor %} /></td>
  <td><input type='checkbox' name='private' value='{{ city.id }}' {% for id in mflags %}{% ifequal id city.id %}checked{% endifequal %}{% endfor %} /></td>
  <td>{{ city.CityName }}</td>
 </tr>
{% endfor %}

MemberProfile Model:

会员资料模型:

class MemberProfile(models.Model):
    Username = models.CharField(max_length=12,unique=True)
    Password = models.CharField(max_length=12)
    SecurityLevel = models.IntegerField()
    AccountExpirationDate = models.DateField()
    CityList = models.TextField()
    Address1 = models.CharField(max_length=30)
    Address2 = models.CharField(max_length=30)
    City = models.CharField(max_length=20)
    State = models.CharField(max_length=2)
    Zip = models.CharField(max_length=10)
    Email = models.EmailField()
    AltEmail = models.EmailField()
    HomePhone = models.CharField(max_length=18)
    BusinessPhone = models.CharField(max_length=18)
    Fax = models.CharField(max_length=18)
    Cell = models.CharField(max_length=18)
    AltPhone = models.CharField(max_length=18)
    PublicVisibleFlags = models.TextField()
    MemberVisibleFlags = models.TextField()
    WhoAmI = models.TextField()
    CompanyName = models.CharField(max_length=30)
    ServicesOffered = models.TextField()
    NumberOfUnits = models.IntegerField()
    SCREIAOffice = models.CharField(max_length=10)
    LastModifyBy = models.CharField(max_length=12)
    LastModifyDate = models.DateField(auto_now=True)

    def __unicode__(self):
        return self.Username

Console Test:

控制台测试:

>>> from screia.core.models import MemberProfile
>>> user = MemberProfile.objects.get(pk=1)
>>> pflags = user.PublicVisibleFlags.split(',')
>>> print pflags
[u'1', u'4', u'7', u'12', u'25']
>>> i = 0
>>> while i < len(pflags):
...   pflags[i] = int(pflags[i])
...   i+=1
... 
>>> print pflags
[1, 4, 7, 12, 25]

Log Value:

日志值:

1

回答by

{% for id in pflags %}{% ifequal id city.id %} ... {% endfor %}

Could it be that idis a string and city.idis an integer?

难道id是一个字符串而city.id是一个整数?

回答by Alex Martelli

The code you've posted would go into infinite loops if either pflags or mflags were non-empty.

如果 pflags 或 mflags 非空,您发布的代码将进入无限循环。

Consider e.g. this snippet from your code:

考虑例如您代码中的这个片段:

  i = 0
  while i < len(pflags):
   pflags[i] = int(pflags[i])

that's it -- end of loop -- noincrementing of iwhatsoever. This is an infinite loop unless len(pflags)is 0!

就是这样——循环结束——没有任何增加i。这是一个无限循环,除非len(pflags)0

Therefore, either you've posted code different from what you're using (in which case it's pretty peculiar for you to expect help;-), or both of those are indeed empty and therefore the inner loops in the template execute 0 times each.

因此,要么您发布的代码与您使用的代码不同(在这种情况下,您期待帮助是很奇怪的;-),或者这两个代码确实是空的,因此模板中的内部循环每个执行 0 次.

I suspect the second possibility obtains, but of course I can't see those xxxFlags values to confirm my suspicion (you can, and should: logthem, for Pete's sake!-).

我怀疑获得了第二种可能性,但当然我看不到那些 xxxFlags 值来证实我的怀疑(你可以,也应该:记录它们,看在皮特的份上!-)。