Re: hidden fields in forms
On Nov 25, 5:06 pm, ChrisK <[EMAIL PROTECTED]> wrote: > I'm just not getting this. I've looked at many examples and can't seem > to pass a hidden value into a form. > > The problem is that I initially get to the form with GET, and then > instantiate the form with PUT. The key that I need is part of the > original URL, and I'm trying to push it down into the PUT data as > well, but failing. > > I have > > (r'^fences/newFence/(?P.*)$', 'newFence'), > > class newFenceForm(forms.Form): > teaser = forms.CharField(max_length=40) > url = forms.CharField() > address = forms.CharField() > range = forms.IntegerField() > auth_id = forms.CharField(widget=forms.HiddenInput) > > def newFence(request, auth_id): > if request.method == 'POST': # if the form has been submitted > form = newFenceForm(request.POST) > if form.is_valid(): > # do the geocoding > print form.cleaned_data['auth_id'] > print form.cleaned_data['teaser'] > print form.cleaned_data['url'] > print form.cleaned_data['address'] > print form.cleaned_data['range'] > # save the object > u = User.objects.filter(auth_id = id) > # show them the new map > return HttpResponseRedirect('../../fences/fenceMap/ > %s'%auth_id) > else: > print auth_id > form = newFenceForm({'auth_id' : auth_id}) > > return render_to_response("map/newfence.html", {'form': form, }) > > I call this with, say, '.../newFence/chris' and the first time through > it prints 'chris' and instantiates the empty form. But where my > template says > > id="id_auth_id" /> > > the string between curlies is always empty. Well..you aren't passing auth_id to the template, you are passing form. auth_id is a field inside your template. Can you post your template? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: hidden fields in forms
> Well..you aren't passing auth_id to the template, you are passing > form. auth_id is a field inside your template. Can you post your > template? Oh, sorry - my oversight. Template is Reminder String: URL of document: Address of interest: Range (in meters): I guess I'm not clear how to access stuff from inside the form! --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: hidden fields in forms
On Nov 25, 6:24 pm, ChrisK <[EMAIL PROTECTED]> wrote: > > Well..you aren't passing auth_id to the template, you are passing > > form. auth_id is a field inside your template. Can you post your > > template? > > Oh, sorry - my oversight. Template is > > > id="id_auth_id" /> > Reminder String: > p> > URL of document: > > Address of interest: > > Range (in meters): > > > > > I guess I'm not clear how to access stuff from inside the form! Check this out: http://docs.djangoproject.com/en/dev/topics/forms/#displaying-a-form-using-a-template Try using the form.as_p or form.as_table methods. If that doesn't work for you, you can render each field yourself. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: hidden fields in forms
On Tue, 2008-11-25 at 15:06 -0800, ChrisK wrote: > I'm just not getting this. I've looked at many examples and can't seem > to pass a hidden value into a form. > > The problem is that I initially get to the form with GET, and then > instantiate the form with PUT. The key that I need is part of the > original URL, and I'm trying to push it down into the PUT data as > well, but failing. You mean POST, not PUT. :-) [...] > id="id_auth_id" /> > > the string between curlies is always empty. Your template doesn't know anything about a variable call auth_id. All it knows about is a variable called "form". The "form" variable has an attribute called auth_id, however. Thus; 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 -~--~~~~--~~--~--~---
Re: hidden fields in forms
OK, thanks for your suggestions (and yes, I meant "POST"). The problem now appears to be how I initialize the form's value of auth_id! If I use just {{form.as_p}} in my template, I get the following html: (Hidden field auth_id) This field is required. This field is required. Teaser: This field is required. Url: This field is required. Address: This field is required. Range: No value is initialized for auth_id ... so perhaps the problem is in how I'm calling the constructor for newFenceForm? --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: hidden fields in forms
On Tue, 2008-11-25 at 18:03 -0800, ChrisK wrote: > OK, thanks for your suggestions (and yes, I meant "POST"). > > The problem now appears to be how I initialize the form's value of > auth_id! Oh, right. I missed that error initially. Yes, you're doing it incorrectly. The first positional argument to a form (which is what you are passing in) is the data from the form submission. Passing in that data means the form is treated as a "bound" form, so the data is validated as part of displaying the form, so that errors can be displayed. Since you only passed in one of the many required values, you get all those errors. What you really want to pass in is initial data. This is done with the "initial" parameter to the form constructor. So your GET branch would look like form = newFenceForm(initial={'auth_id' : auth_id}) I thought "initial" was documented in the forms documentation, but I cannot find it right now, so maybe that's been missed. In any case, that's the way to do it. Regards, Malcolm > If I use just > > {{form.as_p}} > > in my template, I get the following html: > > (Hidden field auth_id) This field is > required. > This field is required. > Teaser: type="text" name="teaser" maxlength="40" /> > This field is required. > Url: id="id_url" /> > This field is required. > Address: name="address" id="id_address" /> > > This field is required. > Range: name="range" id="id_range" /> id="id_auth_id" /> > > No value is initialized for auth_id ... so perhaps the problem is in > how I'm calling the constructor for newFenceForm? > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: hidden fields in forms
> form = newFenceForm(initial={'auth_id' : auth_id}) Awesome. That was exactly what I needed - thanks very much. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---