def editar(request, id): empleado = get_object_or_404(Empleado, id=id) usuario = get_object_or_404(User, id=empleado.user_id) if request.POST: # If the form has been submitted... form = SignUpForm(request.POST, instance=usuario) # A form bound to the POST data empleado_form = EmpleadoForm(request.POST, instance=empleado) if form.is_valid() and empleado_form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data username = form.cleaned_data["username"] password = form.cleaned_data["password"] email = form.cleaned_data["email"] first_name = form.cleaned_data["first_name"] last_name = form.cleaned_data["last_name"] is_staff = form.cleaned_data["is_staff"] usuario.username = username usuario.set_password(password) usuario.email = email usuario.first_name = first_name usuario.last_name = last_name usuario.is_staff = is_staff # At this point, user is a User object that has already been saved # to the database. You can continue to change its attributes # if you want to change other fields # Save new user attributes usuario.save() empleado = empleado_form.save(commit=False) empleado.user = usuario empleado.save() return HttpResponseRedirect('/empleados') # Redirect after POST else: form = SignUpForm(instance=usuario) empleado_form = EmpleadoForm(instance=empleado) #form.fields['password'].widget.attrs['disabled'] = True data = { 'form': form, 'empleado_form': empleado_form } return render_to_response('authapp/signup.html', data, context_instance=RequestContext(request))
def signup(request): if request.POST: # If the form has been submitted... form = SignUpForm(request.POST) # A form bound to the POST data empleado_form = EmpleadoForm(request.POST) if form.is_valid() and empleado_form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data username = form.cleaned_data["username"] password = form.cleaned_data["password"] email = form.cleaned_data["email"] first_name = form.cleaned_data["first_name"] last_name = form.cleaned_data["last_name"] is_staff = form.cleaned_data["is_staff"] # At this point, user is a User object that has already been saved # to the database. You can continue to change its attributes # if you want to change other fields. user = User.objects.create_user(username, email, password) user.first_name = first_name user.last_name = last_name user.is_staff = is_staff # Save new user attributes user.save() empleado = empleado_form.save(commit=False) empleado.user = user empleado.save() return HttpResponseRedirect('/empleados') # Redirect after POST else: form = SignUpForm() empleado_form = EmpleadoForm(request.POST) data = { 'form': form, 'empleado_form': empleado_form } return render_to_response('authapp/signup.html', data, context_instance=RequestContext(request))