You can define a list of valid keys to look for: keys = ['one', 'two', 'three', 'four', 'five'] for key in keys: if not key in request.POST: continue .. do stuff ..
Or you can delete the keys you don't want before looping: for key in ['x', 'y', 'submit']: del request.POST[key] for key, value in request.POST.iteritems(): ... do stuff ... On Jul 27, 8:04 am, Greg <[EMAIL PROTECTED]> wrote: > Jacob, > Yep that was it. If I shouldn't assume anything from the browser. > Then how would I loop through the contents of my data? Below is my > view and template file > > ///////////////// > > This code is in my template file > > {% for a in pad %} > <tr> > <td> > <select name="{{ a.id }}"> > <option value="---">---</option> > <option value="1">1</option> > <option value="2">2</option> > <option value="3">3</option> > <option value="4">4</option> > <option value="5">5</option> > </select> {{ a.size }} -- ${{ a.price }} > </td> > </tr> > {% endfor %} > > ///////////////////// > > This code is my view function: > > def addpad(request): > if request.method == 'POST': > i = 0 > for a in request.POST: > if a[i] != '---': > p = RugPad.objects.get(id=i) > assert False, request.POST > return render_to_response('addpad.html', {}) > > /////////////// > > When the form is posted I want my addpad function to loop through all > the values that have been posted. And if any posted key's contain a > value of something other than '---' then I want to add that to the > cart (Currently I'm just doing an assert statement). If I have other > keys ( such as 'x', 'y', 'submit' ) in my dict then that will cause > the loop to error out. Would you know of any other way to do this so > that I don't have to rely on the contents that come from the web > browser? > > Thanks for your help > > On Jul 27, 9:48 am, "Jacob Kaplan-Moss" <[EMAIL PROTECTED]> > wrote: > > > On 7/27/07, Greg <[EMAIL PROTECTED]> wrote: > > > > It should only have 8 keys (1 through 8). Instead I see the keys 'x' > > > and 'y'? It's causing a problem when I try to loop through my POST > > > data. > > > > Does anybody know why this is happening? > > > You're probably using an <input type="image"> - those send along x/y > > coords of the image click. > > > As a matter of general principle, though, you shouldn't assume > > *anything* about the contents of data that comes from the web browser. > > The browser could be a robot posting data automatically, or you could > > be dealing with a greasemonkey script that's changing your markup, or > > whatever. You need to write your code to deal with extra and/or > > missing fields. > > > Jacob --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---