I created a form in html which returns to a function in views.py in django framework. I wanted to add a button which autofills the password field with a random password. I used pyscript for creating a random password. But the button seems to be returning to the views.py instead of autofilling.
<body bgcolor="white">
<py-script>
import random
password = ''
def mypass():
length = random.randint(8,16)
list1=[]
for i in range(0,length):
list1.append(chr(random.randint(32,126)))
password = ''.join(list1)
pyscript.write('python', password)
</py-script>
<script>
function myfun(){
var a = document.getElementById("password1").value;
var b = document.getElementById("password2").value;
if(a!=b){
document.getElementById("messages").innerHTML="Passwords do not match";
return false;
}
}
</script>
<script>
function myfunction(){
var password = PyScript.interpreter.globals.get('password');
document.getElementById("password1").value = password;
}
</script>
<form onsubmit="return myfun();" action="register" method="post" >
{% csrf_token %}
<p>
<label for="user_name">User Name</label>
<input type="text" name="user_name" id="user_name" placeholder="Antman56." autocomplete="on" >
</p>
<p>
<label for="f_name">First Name</label>
<input type="text" name="f_name" id="f_name" placeholder="Dave" autocomplete="on" >
</p>
<p>
<label for="l_name">Last Name</label>
<input type="text" name="l_name" id="l_name" placeholder="Lee" autocomplete="on" value="keyan" >
</p>
<p>
<label for="email">Email ID</label>
<input type="email" name="email" id="email" placeholder="[email protected]" autocomplete="on" >
</p>
<p>
<label for="password1">Password</label>
<input type="password" name="password1" id="password1" placeholder="123$aBc" >
</p>
<p>
<label for="password2">Confirm Password</label>
<input type="password" name="password2" id="password2" placeholder="123$aBc" ><span id="messages" style="color:red"></span>
</p>
<div id="python"></div>
<button onclick="mypass()">create random password</button>
<button onclick="myfunction()">fill the random password</button>
<input type="submit" value="Submit" >
</form>
when I click the ‘create random password’, I expect the mypass function in pyscript to execute and when I click the ‘fill the random password’ button, I want to execute the myfunction function and autofill my password field with the random password previously generated.