On Feb 11, 4:11 pm, gbd <ganit.bar...@gmail.com> wrote: > Another point (sorry I should have said this in my original post) > > My form is bound > i.e. in my view > form = RecipeForm(request.POST) > I don't believe that I can use initial in this case? > > please let me know if I've misunderstood > > thanks again! > > On Feb 11, 11:06 am, gbd <ganit.bar...@gmail.com> wrote: > > > Thanks! > > > A follow up question - > > > Would i exclude that field in my class description? > > i.e. > > > class RecipeForm(forms.ModelForm): > > class Meta: > > model = Recipes > > exclude = ('user',) >
The standard Django idiom for a form view is like this: if request.method='POST': form = MyForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('whatever') else: form = MyForm(initial=initial_data) return render_to_response(template, {'form':form}) So the form is only bound the second time through, when you return with a POST. The first time through, when you initially render the form for the user to fill in, you don't have a POST, and the form isn't bound. It's there - the second-to-last line in my example - that you should pass the initial dictionary. And in response to your other question, no you shouldn't exclude that from the form, or this method won't work. There is an alternative method, though. If you don't want the user field to appear at all, exclude the field, don't bother with initial, and set the user on save. So in line 4 above, you would do this: instance = form.save(commit=False) instance.user = request.user instance.save() -- DR. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---