Ask coding questions

← Back to all posts
Is there a way to link the textbox in html to the database in python?
51LV3RC0D3R (12)

So I have created a login page, but I don't know how to make the username and pwd to the database in python. Is there a way I can do that?

Answered by CosmicBear (45) [earned 5 cycles]
View Answer
Comments
hotnewtop
CosmicBear (45)

In this example the the database key "loginInfo" is a list that contains dictionaries that contain a key (username) with the value (password).

@app.route('/login',methods = ['POST', 'GET'])
def login():
    if flask.request.method == "POST":
        user = flask.request.form['username']
        pwd = flask.request.form["pwd"]
        data = db["loginInfo"]
        data.append({user:pwd})
        db["loginInfo"] = data
      return flask.redirect(flask.url_for('success',name = user))
51LV3RC0D3R (12)

@CosmicBear I did that, but now its giving me this weird error even when I run it from repl.it and not open it in a new tab:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.

CosmicBear (45)

@51LV3RC0D3R that means that there is an error somewhere in your application.

QuantumCodes (25)

Shouldnt you also do app.run('0.0.0.0',port=8080) in pythonand in html, the <html> should be <!DOCTYPE html>

Coder100 (18903)

you can make a post request to send data over to the python backend.
https://pythonbasics.org/flask-http-methods/