views:

499

answers:

2

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.

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:

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
+1  A: 

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

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 -- no incrementing of i whatsoever. This is an infinite loop unless len(pflags) is 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.

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

Alex Martelli
Hrm, good point. Well I changed that. I know that its getting the info somewhere because it fills in the rest of the form. For some reason pflags and mflags seem to be empty and i'm not sure why. However I went to the shell and ran the same query and did u.PublicVisibleFlags and it printed out the list...so i'm not really sure where the data is getting lost
The.Anti.9
If you look at my post again, I added a shell test that I did that proves that pflags and mflags shouldn't be empty. I used the same code in the shell and results came up. For some reason its not working though. Not sure.
The.Anti.9
I don't see the logging call to show their values in the actual run and the result of that logging call -- why don't you do that, as I suggested from the start?
Alex Martelli
Ok, I tried the logging. It wrote the first value to a file, and it came out as one as it should have now, but for whatever reason, the checkbox still doesn't get checked when I go back to that page.
The.Anti.9
+1  A: 
{% for id in pflags %}{% ifequal id city.id %} ... {% endfor %}

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

Dan
Or one is a string and the other is unicode -- this has cause a frustration for me in the past.
ashchristopher
Good point! Definitely a trap worth pointing out.
Dan