I'm trying to create login page and register page for personal project in Python / Flask but I am facing this error in vscode terminal, when I try to access Register endpoint:
"werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'Register'. Did you mean 'register' instead?
Here in the code only the hello page works, if I include /register or /login in endpoint it throws:
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.
app.py
import mailbox
from flask import Flask,url_for,request, render_template,redirect
import os
import psycopg2
from flask_mail import Mail
from flask_mail import Message
app = Flask(__name__)
connection = psycopg2.connect(
database="***",user="***",password="*****",host="localhost",port=5432
)
cursor = connection.cursor()
@app.route('/hello',methods=["GET","POST"])
def hello():
if(request.method =="GET" or request.method == "POST"):
postgre_str="select * from play.player_list"
cursor.execute(postgre_str)
player_records = cursor.fetchall()
if (request.method == "POST"):
player_name=request.form.get("playername")
postgre_str="insert into play.player_list(player_name) values('{0}')".format(player_name)
cursor.execute(postgre_str)
connection.commit()
cursor.close()
connection.close()
return render_template('index.html',player_records=player_records)
@app.route('/', methods=['GET', 'POST'])
def login():
error = None
if request.method == 'POST':
if request.form['username'] != 'admin' or request.form['password'] != 'admin':
error = 'Invalid Credentials. Please try again.'
else:
return redirect(url_for('hello'))
return render_template('login.html', error=error)
@app.route('/register', methods=['POST','GET'])
def register():
# Perform any necessary actions here
# mail = mailbox(app)
return render_template('register.html',status='ok')
# return redirect(url_for('register',status='ok'))
if __name__ == '__main__':
app.run(debug=True)
login.html:
<html>
<head>
<title>Play Soccer - login page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="static/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<div class="container">
<h1>Please login</h1>
<br>
<form action="/hello" method="post">
<input type="text" placeholder="Username" name="username">
<input type="password" placeholder="Password" name="password">
<input class="btn btn-default" type="submit" value="hello">
</form>
{% if error %}
<p class="error"><strong>Error:</strong> {{ error }}
{% endif %}
<a href="{{url_for('Register')}}">Register</a>
</div>
</body>
</html>
index.html:
<!DOCTYPE html>
<html>
<head>
<title>GIF Display Example</title>
</head>
<body>
<h1>GIF Display Example</h1>
<img src="{{ url_for('static', filename='image.gif') }}" alt="image.gif" width="1200" height="500">
<form method='POST' action="{{url_for("hello")}}">
<label for="fname">player choice </label><br>
<p>
<select id="playername" name="playername">
{%for i in player_records %}
<option value={{i[0]}}>{{i[1]}}</option>
{%endfor%}
</select>
</p>
<button type="submit">enter</button>
</form>
<form method='POST' action="">
<label for="username">Username:</label><br>
<input type="text" id="username" name="username"><br>
<label for="pwd">Password:</label><br>
<input type="password" id="pwd" name="pwd"><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Register.html
<html>
<head>
<title>Play Soccer - register page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="static/bootstrap.min.css" rel="stylesheet" media="screen">
</head>
<body>
<h1>Register</h1>
<form method="POST" action="/register.html">
<input type="email" id="email" name="email" placeholder="Email address">
<input type="password" id="pwd" name="pwd" placeholder="Password">
<input type="submit" value="Submit">
</form>
</body>
</html>