At the moment I can successfully add the record by manually typing username & passphrase on HTML form. How can I pass the passphrase (dinopsk
) from dinopass1
function to the form passphrase input?
app.py
:
def dinopass1():
response = requests.get("https://www.dinopass.com/password/simple")
dinopsk = response.text
#print(dinopsk)
@app.route("/add_user", methods=["GET", "POST"])
def add_user():
if request.method == "POST":
headers = {"Authorization": f"{xxxx}"}
new_user = {
"username": request.form["name"],
"passphrase": request.form["passphrase"]
}
requests.post(
"API_URL", json=new_user, headers=headers
)
return redirect(url_for("add_user("))
add.html
:
<form action="{{url_for('add_user')}}" method="POST">
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" name="name" required="1">
</div>
<div class="form-group">
<label>Passphrase:</label>
<input type="text" class="form-control" name="passphrase" required="1">
</div>
<div class="form-group">
<button class="btn btn-primary mt-4" type="submit">Add</button>
</div>
</form>
It's hard to understand the question as it looks like you're not displaying the form to the user, aka GET
method. You only shared the section where you get the form values, aka POST
method. But here it goes:
If you want to display the password in the form, you need to use the value
attribute of the <input> tag and set it to the value you get from dinopass.
<div class="form-group">
<label>Passphrase:</label>
<input type="text" class="form-control" name="passphrase" value="somepass" required="1">
</div>
You can go two ways with this: Either use some Javascript to set the value attribute. Or if you want to do it with Flask, you render the template with something like:
app.py
:
def dinopass1():
response = requests.get("https://www.dinopass.com/password/simple")
dinopsk = response.text
return dinopsk
@app.route("/add_user", methods=["GET", "POST"])
def add_user():
if request.method == "POST":
headers = {"Authorization": f"{xxxx}"}
new_user = {
"username": request.form["name"],
"passphrase": request.form["passphrase"]
}
requests.post(
"API_URL", json=new_user, headers=headers
)
return redirect(url_for("add_user("))
else:
new_pass = dinopass1()
return render_template('add.html', password=new_pass)
add.html
:
<form action="{{url_for('add_user')}}" method="POST">
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" name="name" required="1">
</div>
<div class="form-group">
<label>Passphrase:</label>
<input type="text" class="form-control" name="passphrase" value={{ password }} required="1">
</div>
<div class="form-group">
<button class="btn btn-primary mt-4" type="submit">Add</button>
</div>
</form>