gpt4 book ai didi

python - Django 模板 ifequal 标签

转载 作者:太空狗 更新时间:2023-10-30 02:35:34 27 4
gpt4 key购买 nike

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

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

查看:

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')

模板剪辑:

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

成员(member)资料模型:

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

控制台测试:

>>> 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]

日志值:

1

最佳答案

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

难道id是字符串,city.id是整数?

关于python - Django 模板 ifequal 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2099064/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com