Reputation: 707
I need some of your help please,
I'm working with pysftp
this is working great but now I'm trying to make it work to my project in Django
worked great in console but I want to get the data from a form
so I won't need to use the console
to do that.
here's my view:
def sftp_form(request):
if request.method == 'POST':
form = sftpForm(request.POST or None)
if form.is_valid():
data = form.cleaned_data
host = data['host']
usuario = data['usuario']
clave = data['clave']
print host
print usuario
print clave
else:
form=sftpForm()
return render(request, 'sftp.html', {'form':form})
def SFTP_subir():
host = raw_input('ingrese el host: ') # I want form's host here.
usuario = raw_input('ingrese el usuario: ')# I want form's usuario here.
clave = raw_input('ingrese la clave: ')# I want form's clave here.
try:
transferencia = sftp.Connection(host=host, username=usuario, password=clave)
remotepath= 'remotepath'
localpath="mylocalpath"
transferencia.put(localpath,remotepath)
print ('\n' + 'Sucess.')
except Exception, e:
print str(e)
as you can see in my code sftp_subir()
it's asking me for host,usuario and clave from console, but I want to make it work with sftp_form()
host,usuario and clave.
Upvotes: 1
Views: 2953
Reputation: 499
In your view:
def sftp_form(request):
if request.method == 'POST':
form = sftpForm(request.POST or None)
if form.is_valid():
data = form.cleaned_data
host = data['host']
usuario = data['usuario']
clave = data['clave']
print host
print usuario
print clave
SFTP_subir(host, usuario, clave) # here you invoke the function, passing variables as arguments
else:
form=sftpForm()
return render(request, 'sftp.html', {'form':form})
Then refactor your function to receive those params:
def SFTP_subir(host, usuario, clave):
try:
transferencia = sftp.Connection(host=host, username=usuario, password=clave)
remotepath= 'remotepath'
localpath="mylocalpath"
transferencia.put(localpath,remotepath)
print ('\n' + 'Sucess.')
except Exception, e:
print str(e)
Upvotes: 1
Reputation: 4422
you can do the
sftp.connect(...)
...
<4 lines following>
inside the request.method == "POST" block instead of your print statements.
Upvotes: 0
Reputation: 53734
There seem to be a slight mixup here, you can't use raw_input
in a django web app. If you using Django as a CLI you can't use an HTTP request. As @sayse suggested in the comments, if you are using a view in a web app all you need to do is to define your second function to be one that accepts paramers
def sftp_form(request):
if request.method == 'POST':
form = sftpForm(request.POST or None)
if form.is_valid():
data = form.cleaned_data
host = data['host']
usuario = data['usuario']
clave = data['clave']
SFTP_subir(hosts, usuario,clave)
else:
form=sftpForm()
return render(request, 'sftp.html', {'form':form})
def SFTP_subir(hosts, usuario,clave):
try:
transferencia = sftp.Connection(host=host, username=usuario, password=clave)
remotepath= 'remotepath'
localpath="mylocalpath"
transferencia.put(localpath,remotepath)
print ('\n' + 'Sucess.')
except Exception, e:
print str(e)
Once you make this code you still have a long way to go because your SFTP method doesn't return any usefull response.
Upvotes: 2