Reputation: 41
I want to populate values into 4 text fields based on the value given in the input field. when control comes out of the input field, a function getcredentials() is called which in turn calls a python code. python code retrieves the 4 required values from an excel sheet and returns the result as a dictionary. I have tried to set those 4 values from the result dictionary in the following way. please correct me.
script :
<script>
function getcredentials() {
var x = document.index.AWSID.value;
console.log(x)
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.index.APPUSERNAME.value = this.responseText['AppUsername'];
document.index.APPPASSWORD.value = this.responseText['AppPassword'];
document.index.RDPUSERNAME.value = this.responseText['RdpUsername'];
document.index.RDPPASSWORD.value = this.responseText['RdpPassword'];
}
};
xhttp.open('POST', 'getcredentials', true);
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
/*xhttp.send(x);*/
xhttp.send("x="+encodeURIComponent(x));
}
</script>
python code :
def getCredentials(request):
AwsId = request.POST.get('x')
result = {'RdpUsername' : 'Opsadmin',
'RdpPassword' : '--C0nt@1ns^Ph3n%l@l@n1n3--',
'AppUsername' : 'Admin',
'AppPassword' : '[email protected]'}
result['RdpPassword'] = result['RdpPassword'] + AwsId[-5:]
result['AppPassword'] = result['AppPassword'] + AwsId[0:3]
response = HttpResponse(result)
return response
Upvotes: 1
Views: 133
Reputation: 723
You should be using JsonResponse(result)
rather than HttpResponse
. Something like:
from django.http import JsonResponse
def getCredentials(request):
AwsId = request.POST.get('x')
result = ...
response = JsonResponse(result)
return response
Upvotes: 1