I'm trying to make a database with two linked tables Info and User, I also want to be able to post to the database online, whenever I run the code below, after I click submit on my index page, I get given an error : 'The method is not allowed for the requested URL.', and nothing is posted into my database. Im new to python and SQLAlchemy, so I'm unsure where the error is occurring. This is my python code:
from flask import Flask, render_template, request, redirect, url_for
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship, backref, sessionmaker
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:////Users/me/attempt1/attempt.db"
app.debug = True
db = SQLAlchemy(app)
engine = create_engine('sqlite:////Users/me/attempt1/attempt.db')
connection = engine.connect()
Base = declarative_base()
session = sessionmaker(bind=engine)
class Info(Base):
__tablename__ = 'info'
id = Column(Integer, primary_key=True)
username = Column(String(80), unique=True, nullable=False)
age = Column(Integer, nullable=False)
country = Column(String(120), nullable=False)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
email = Column(String(225), unique=True, nullable=False)
username = relationship('Info', backref=backref('users', lazy='True'))
password = Column(String(225), nullable=False)
# this will be the home page
Base.metadata.create_all(engine)
session = session()
@app.route('/')
def index():
return render_template('dashboard.html')
# sign up page
@app.route('/signup', methods=['POST'])
def signup():
info = Info(request.form['username'], request.form['age'], request.form['country'])
user = User(request.form['email'], request.form['password'])
session.add(info)
session.add(user)
session.commit()
return redirect(url_for('login'))
@app.route('/login')
def login():
return 'You are now logged in'
if __name__ == '__main__':
app.run()
this is my html code for dashboard.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sign Up Today</title>
</head>
<body>
<form method ="signup" action="/signup">
<label>Email:</label>
<input id="email" name="email" type="text"/>
<label>Username:</label>
<input id="username" name="'username" type="text"/>
<label>Password:</label>
<input id="password" name="password" type="'text"/>
<label>Age:</label>
<input id="'age" name="age" type="text"/>
<label>Country:</label>
<input id="country" name="country" type="text"/>
<input type="submit"/>
</form>
</body>
</html>