0

I am having issues linking my flask program to Postgre SQL

import os

from flask import Flask, session
from flask_session import Session
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from flask import Flask, render_template, request

app = Flask(__name__)

# Check for environment variable
if not os.getenv("DATABASE_URL"):
    raise RuntimeError("DATABASE_URL is not set")

# Configure session to use filesystem
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"

# Set up database
engine = create_engine(os.getenv("DATABASE_URL"))
db = scoped_session(sessionmaker(bind=engine))

@app.route("/")
def index():
   return render_template("index.html")

@app.route("/register.html",methods=['GET','POST'])
def register():
    return render_template("register.html")
    if request.method=='POST':
        name=request.form['username']
        password=request.form['password']
        connection = mysql.get_db()
        cursor = connection.cursor()
        query="INSERT INTO 'User Details'(username,password) VALUES(%s,%s)"
        cursor.execute(query,(username,password))
        connection.commit()

@app.route("/login.html")
def login():
    return render_template("login.html")

Register.html

<html>
<head>
<title>Project1</title>
</head>
<form method="POST">
  <input type="username" name="username" placeholder="Username">
  <input type="password" name="password" placeholder="Password">
  <input type="submit" value="Register">
</form>
</html>

I have already specified the DATABASE_URl when setting up with the credentials given by Heroku. I cannot manage to store the username and password into the database. The register.html page contains a form with username input field and password input field. Help will be appreciated.

Thanks

1 Answer 1

0

Don't you need an action attribute here <form method="POST">? To tell the browser where to go. Here is the MDN doc on form. This register.html gives a 405 error when the button is clicked (in IE11, FF63).

ADDED-->
Assuming you want to post the data in register.html to the register route in application.py, you need to change this <form method="POST"> to this <form action="/register" method="post">.

While double checking this syntax, I see a couple of other problems in application.py:

  • The name of the route should be "register", not "register.html", as in @app.route("/register,methods=['GET','POST'])
  • If you return render_template("register.html") first thing in the function, it will never do the update, it will just display the form again. You probably want to do that only if request.method is "GET".
6
  • Didn't I already write that? Dec 8, 2018 at 5:12
  • It is not in the<form> element in the Register.html code posted here. Dec 8, 2018 at 10:50
  • i must be dumb or something, could you just rewrite the working code here/ Dec 8, 2018 at 13:37
  • The something is "learning"! You can't rush learning. Answer edited, I hope it helps. Dec 8, 2018 at 17:13
  • I am now getting hang of it now thanks. Dec 8, 2018 at 17:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .