Mark, You should really use Forms and FormSets--they'll make this problem essentially go away.
http://docs.djangoproject.com/en/dev/topics/forms/#topics-forms-index http://docs.djangoproject.com/en/dev/topics/forms/formsets/ -Jeff On Jan 12, 12:46 am, Mark Jones <mark0...@gmail.com> wrote: > I have some fields on my page that have more than one attribute for > simplification, lets say two attributes > > answer = {'txt':'something', 'allowblank':0} > > laid out on the page as: > {% for answer in quiz_question.answers %} > <input type="text" size='64' name='answer' value="{{ answer.txt }}" / > > <input type="hidden" name='allowblank' > value="{{ answer.allowblank }}" /> > {% endfor %} > > the first is pulled from an input field, and if !allowblank, needs to > have some text. > > I write some OnSubmit() javascript on my page to check this field, and > if it is blank, check the allowblank attribute in the following hidden > field to see if this is OK. If is isn't ok, I ask the user "you sure > you wanna leave this field blank?" If the user says "YES, I want to > leave it blank, the JS changes allowblank to 1 and we submit the form. > > If the backend gets the form and the field is blank, but the > allowblank is true, then I know the user said OK, no problem, I let it > be blank, otherwise I validate it on the server side (in case they use > noscript like I do) and if allowblank=0 and the field is blank, I send > the form back to them with a checkbox like this > > {% for answer in question.answers %} > <input type="text" size='64' name='answer' value="{{ answer.txt }}" / > > <input type="Checkbox" name='allowblank' > value="{{ answer.allowblank }}" /> I > really want to leave this field blank > {% endfor %} > > My problem comes about because a nice list of dict objects has been > broken apart by the html form and needs to be reassembled. While I > can do this like so: > > for i in range(len(qqanswers)-1,0, -1): > qqanswers[i] = {'txt':answers[i], 'allowblank':int > (allowblank[i])} > if answers[i] == '' and allowblank[i]: > del qqanswers[i] > > I don't like how ugly this looks. I tried changing the names of the > input objects to: > > answers.txt and answers.allowblank, but that didn't work, I got POST > lists back that were names answers.txt and answers.allowblank that > looked like: > > <QueryDict: {u'answers.allowblank': [u'0', u'0', u'0', u'0'], > u'answers.answer': [u'1', u'2', u'3', u'4']}> > > of course all of this ignores the fact that checkboxes that aren't > checked aren't sent back to the server anyway which is where the need > for a more elegant solution comes from in the first place...... > > Is there a nice elegant way to solve this, or does it just have to be > done the way I've done it...... --~--~---------~--~----~------------~-------~--~----~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---