最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin3浏览0评论

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>
发布评论

评论列表(0)

  1. 暂无评论