Search code examples
pythonmysqlflaskpythonanywhere

Pythonanywhere [Flask] - Creating a MySQL database


I would like to create a database that stores all passwords. I followed this tutorial and it worked when I made exactly the same as they did, but now I want to make my own database. I already made a database actually and it shows the right tables and "describe (tablename)" also works, but when I do select * from (tablename);, it returns Empty set (0.00 sec). That means that nothing is stored but I did post some password on my website.

This is the flask_app.py code:

from flask import Flask, render_template, request, redirect
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config["DEBUG"] = True

SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
    username="*******", //I filled these in
    password="*******",
    hostname="*******.mysql.pythonanywhere-services.com",
    databasename="******$wachtwoorden",
)
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
db = SQLAlchemy(app)

class Wachtwoord(db.Model):

    __tablename__ = "wachtwoorden"

    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(80))

@app.route("/", methods=["GET", "POST"])

def main():

    if request.method == "GET":
        print("in request.method = get")
        return render_template("main_page.html")

    if (request.form["Knop1"] == "1912"):
        print ('Het wachtwoord is geraden:', request.form["Knop1"])
        return redirect("http://tegelizr.nl/tegeltjes/dat-klopt-als-een-bus.png")

    else:
        print ('Iemand heeft een het wachtwoord geraden:', request.form["Knop1"])
        return render_template("main_page.html")

    wachtwoord = Wachtwoord(content=request.form["Knop1"])
    db.session.add(wachtwoord)
    db.session.commit()

This is in my main_page.html

<form method="POST">
     <p>
        <center>
           <br><br><br><h2>Wat is het wachtwoord?</h2><input type="text" name="Knop1" value = "" />
           <br><br><input type="image" src="http://www.kryshiggins.com/wordpress/wp-content/themes/KrystalsNewMergedThemeOMG/Krys_Images/Next_Arrow.png" alt="Submit" width="30" height="30"><br><br><br>
        </center>
    </form>

So how do I store the passwords the right way? Sorry, but wachtwoord means password. Oh and it might be a bit confusing, because you just have to give the right password, which is 1912, then why do I want to store the attempted passwords? Well, it's just a test for me and if this works I know how to do it in the future.

Thanks.


Solution

  • You are doing a return to render your template before it is able to call your database insert. Try this:

    def main():
    
        if request.method == "GET":
            print("in request.method = get")
            return render_template("main_page.html")
    
        if (request.form["Knop1"] == "1912"):
            print ('Het wachtwoord is geraden:', request.form["Knop1"])
            wachtwoord = Wachtwoord(content=request.form["Knop1"])
            db.session.add(wachtwoord)
            db.session.commit()
            return redirect("http://tegelizr.nl/tegeltjes/dat-klopt-als-een-bus.png")
    
        else:
            print ('Iemand heeft een het wachtwoord geraden:', request.form["Knop1"])
            return render_template("main_page.html")