On 3/5/09, Chr1s <[email protected]> wrote:
>
> hi folks,
>
>     I have a customized form, I want to it can be displayed, if user
> made a change, it can be submitted and saved. what should I do? the
> form was made from 2 different models.
>
>
> here is my code:
> =================forms.py===================
> class ChangePersonalInfo(forms.Form):
>     email = forms.EmailField(required = False)
>     firstname = forms.CharField(required = True)
>     lastname = forms.CharField(required = True)
>     birthday = forms.DateField(input_formats=['%Y-%m-%d'],required =
> False)
>     gender = forms.ChoiceField(choices = GENDER_CHOICES)
>     country = forms.CharField(required = True,initial='中国')
>     province = forms.CharField(required = True,initial = '北京')
>     city = forms.CharField(required = True,initial = '北京')
>     address_home = forms.CharField(widget = forms.Textarea,required =
> True)
>     address_work = forms.CharField(widget = forms.Textarea,required =
> True)
>     line_phone = forms.CharField(required = False)
>
>
>
>     def save(self,user):
>         lastname = self.cleaned_data.get('lastname')
>         firstname = self.cleaned_data.get('firstname')
>         email = self.cleaned_data.get('email')
>         gender = self.cleaned_data.get('gender')
>         country = self.cleaned_data.get('country')
>         province = self.cleaned_data.get('province')
>         city = self.cleaned_data.get('city')
>         line_phone = self.cleaned_data.get('line_phone')
>         address_home = self.cleaned_data.get('address_home')
>         address_work = self.cleaned_data.get('address_work')
>         """
>         update process
>         """
>         myuser = User.objects.get(username = user)
>         myprofile = Profile.objects.get(user = myuser)
>
>         myuser.last_name = lastname
>         myuser.first_name = firstname
>         myuser.email = email
>
>         myprofile.gender = gender
>         myprofile.country = country
>         myprofile.city = city
>         myprofile.province = province
>         myprofile.line_phone = line_phone
>         myprofile.address_home = address_home
>         myprofile.address_work = address_work
>
>         myuser.save()
>         myprofile.save()
>
>
> ==================views.py======================
> def change_personal_info(request,template):
>     """
>     change personal information
>     """
>
>     if request.method == 'POST':
>         form = ChangePersonalInfo(request.POST)
>         if form.is_valid():
>             #profile = profile.
>             form.save(request.POST,profile)
>             return HttpResponseRedirect('%sdone/' % request.path)
>
>     else:
>         myuser = User.objects.get(username = request.user)
>         profile = Profile.objects.get(user=myuser)
>         form = ChangePersonalInfo()
>
>     context = RequestContext(request,{'form':form})
>     return render_to_response(template,context_instance=context)
>
>
> >
>

Take a look at ModelForms because they really make this pattern a
solved problem.

Alex

-- 
"I disapprove of what you say, but I will defend to the death your
right to say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to