def InfoToUpdate(request): if request.method == 'GET': user = request.GET.get('user') if user: title="更新用户" data = Curd.objects.get(name=user) form=NewRecordForm(initial = { 'name':data.name, 'email':data.email, 'homePage':data.homePage, 'age':data.age, 'isMarried':data.isMarried }) form.fields['name'].widget.attrs['readonly'] = True return render_to_response('curd/userInfo.html', RequestContext(request,locals())) elif request.method == 'POST': user = request.POST.get('name') data = Curd.objects.get(name = user) form=NewRecordForm(request.POST,instance=data) if form.is_valid(): form.save() data = Curd.objects.all() return backToHomePage(request,data,'更新成功') else: return render_to_response('curd/userInfo.html', RequestContext(request,locals()))
def Create(request): if request.method == 'GET': title="新建用户" form = NewRecordForm() return render_to_response('curd/newUser.html', RequestContext(request,locals())) elif request.method =='POST': form=NewRecordForm(request.POST) if form.is_valid(): obj,created=Curd.objects.get_or_create( name=form.cleaned_data['name'], email=form.cleaned_data['email'], age=form.cleaned_data['age'], homePage=form.cleaned_data['homePage'], isMarried=form.cleaned_data['isMarried'] ) if created: message="Success!" else: message="Record exists!" data = Curd.objects.all() return backToHomePage(request,data,message) else: return render_to_response('curd/newUser.html', RequestContext(request,locals()))