python - How to pass variable to Flask HTML form - Stack Overflow

At the moment I can successfully add the record by manually typing username & passphrase on HTML fo

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(";)
    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>

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/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>
Share Improve this question edited Mar 22 at 17:33 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Mar 22 at 16:33 bickyzbickyz 432 silver badges9 bronze badges 1
  • i want to pass the passphrase that has been acquired from dinopass API to the flask HTML form (passphrase input box) – bickyz Commented Mar 22 at 17:39
Add a comment  | 

1 Answer 1

Reset to default 1

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/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>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744308472a4567832.html

相关推荐

  • python - How to pass variable to Flask HTML form - Stack Overflow

    At the moment I can successfully add the record by manually typing username & passphrase on HTML fo

    7天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信