On Sat, 2007-07-28 at 19:44 -0700, Greg wrote: > I tried to implement the above view. However, I'm having errors. > Again i think it has to do with the u character in my dict. Here is > my view: [... snipped ...] > This is the error I get: > > MultiValueDictKeyError at /rugs/cart/addpad/ > "Key 1 not found in <MultiValueDict: {u'1': [u'0'], u'3': [u'0'], > u'2': [u'1'], u'5': [u'0'], u'4': [u'0'], u'7': [u'0'], u'6': [u'0'], > u'8': [u'0']}>" > > It can't find the key 1...because the key is actually u'1'??? I don't > know where the u is coming from.
Form data values are always strings. The u'1' means it's a Unicode string in Python, but it's still a string. The fact is, we have no way of knowing, when somebody sends the byte for "1", whether they mean a string or an integer, so we don't guess. This is normally why helper functions, such as a newforms.Form subclass are normally used: it can validate that the data is what you are expecting (integers in this case) and convert them to the correct Python types. If you can't model the submitted data as a form somehow (e.g. if it's not regular enough), you are going to have to convert the values to the right Python types yourself. Form data names are always strings (essentially), so converting them won't be too useful. Possibly the solution in your case is to simply coerce the id values to Unicode strings (even normal strings will do) before comparison. So something like if request.POST[str(a.id)] != 0: # ... Regards, Malcolm --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---