[Python] How to pass variable to Flask HTML form

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    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>

    Continue reading...

Compartilhe esta Página