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:
def addpad(request):
if request.method == 'POST':
pads = request.session.get('pad', [])
pad = RugPad.objects.all()
#assert False, pad
for a in pad:
if request.POST[a.id] != 0:
opad = RugPad.objects.get(id=a)
s = opad.size
p = opad.price
pads.append({'size': s, 'price': p, 'quanity':
request[a]})
request.session['pad'] = pads
return HttpResponseRedirect("/rugs/cart/checkout")
///////////
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.
Thanks
On Jul 28, 10:25 am, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> Right... because, unless I'm misunderstanding your code... you're
> creating a select drop down for each of your RugPad entires with the
> RugPad id. So you know that the request.POST is going to have entries
> for each id...
>
> But..I think it should be this:
> opads = RugPad.objects.all()
> for a in opads:
> if request.POST[a.id] != 0:
> opad = RugPad.objects.get(id=a)
> s = opad.size
> p = opad.price
> pads.append({'size': s, 'price': p})
>
> So now you'd be iterating through each of your rug pads, and checking
> to see if the corresponding drop down had a non-zero value selected,
> instead of iterating all of the request.POST keys. This way you can
> have other items in your form if needed.
>
> On Jul 28, 10:58 am, Greg <[EMAIL PROTECTED]> wrote:
>
> > Carole,
> > So your saying that I should do:
>
> > opad = RugPad.objects.all()
> > for a in opad.id
> > if request[a] != 0
> > .....#add to session
>
> > On Jul 28, 8:43 am, "[EMAIL PROTECTED]"
>
> > <[EMAIL PROTECTED]> wrote:
> > > Greg... I'm curious as to why you're iterating over the entire
> > > request.POST when you could just use the id of each pad that you've
> > > previously passed to your template to retrieve it from the
> > > request.POST dict, as that is what you've named your select statements
> > > with?
>
> > > While iterating through all of the request.POST key/value pairs works,
> > > if you choose to later add any other inputs to your screen that aren't
> > > drop downs for your CarpetPads, you'll have to add special code for
> > > each of them so that your code won't attempt to add a RugPad for those
> > > key/value pairs. Just wanted to point that out, in case you might
> > > need to do that later.
>
> > > Carole
>
> > > On Jul 28, 9:15 am, Greg <[EMAIL PROTECTED]> wrote:
>
> > > > Nathan,
> > > > Thanks for your help...I got it working. This is what I used:
>
> > > > if values != list("0"):
>
> > > > Is that what you were recommending? Because I couldn't convert a list
> > > > (values) to a int. Since values was a list I decided to convert my
> > > > int to a list and that worked. Can you tell me what the u stands for
> > > > when i do a 'assert False, values': It returns a [u'0']. Why doesn't
> > > > it just return a ['0']? Also, it there anyway that I can convert the
> > > > 'values' variable to a int so that I can do a comparison without
> > > > converting the 0 to a list?
>
> > > > Thanks
>
> > > > On Jul 28, 2:57 am, Nathan Ostgard <[EMAIL PROTECTED]> wrote:
>
> > > > > To illustrate with the Python shell:
>
> > > > > >>> 0 == "0"
> > > > > False
> > > > > >>> 0 == int("0")
>
> > > > > True
>
> > > > > On Jul 27, 11:10 pm, Sean Perry <[EMAIL PROTECTED]> wrote:
>
> > > > > > On Jul 27, 2007, at 10:36 PM, Greg wrote:
>
> > > > > > > AssertionError at /rugs/cart/addpad/
> > > > > > > [u'0']
>
> > > > > > > Does that mean that the value is 0? below is my view function and
> > > > > > > template code:
>
> > > > > > That little 'u' in front of the '0' means unicode so the value is
> > > > > > the
> > > > > > unicode string "0" not the number zero. Very different as far as
> > > > > > Python is concerned. You may be used to other scripting languages
> > > > > > that auto-convert. Python is not one of them.
>
> > > > > > try:
> > > > > > num = int(values)
> > > > > > if num == 0:
> > > > > > deal_with_zero()
> > > > > > else:
> > > > > > deal_with_number(num)
> > > > > > except ValueError:
> > > > > > # hmm, values is a string, not a number
> > > > > > deal_with_a_string(values)
>
> > > > Hope that helps.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---