Reputation: 25
I write this forum and I want to validate user name and password but I have result.Method Not Allowed
hello.html
<html lang='en'>
<head>
<meta charset="utf-8" />
<title>Hello</title>
</head>
{% if error %}
{{ error }}
{% endif %}
<body>
Hello
</body>
</html>
The method is not allowed for the requested URL.
<!DOCTYPE html>
<!--[if (gte IE 9)|!(IE)]><!--> <html lang="en"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<title>welcome</title>
<link href="{{ url_for('static', filename='style.css') }}" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="container">
<section id="content">
<form action="/login" method="post">
<h1>Login Form</h1>
<div>
<input type="text" placeholder="Username" required="" name="username" />
</div>
<div>
<input type="password" placeholder="Password" required="" name="password" />
</div>
<div>
<input type="submit" value="Log in" />
</div>
</form><!-- form -->
</section><!-- content -->
</div><!-- container -->
</body>
</html>
I write a forum:
in app.py:
from flask import Flask
from flask import request
from flask import render_template, redirect, url_for, request
APP = Flask(__name__)
@APP.route('/')
def login1():
return render_template('login1.html')
@APP.route('/login', 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('home'))
return render_template('hello.html', error=error)
if __name__ == '__main__':
APP.debug=True
APP.run()
I can't validate the input? how can help me please
Upvotes: 2
Views: 27778
Reputation: 723
I modified your code app.py code to the below, along with your current hello.html and login1.html this is working fine.
from flask import Flask
from flask import request
from flask import render_template, redirect, url_for, request
APP = Flask(__name__)
@APP.route('/')
def login1():
return render_template('login1.html')
@APP.route('/login', 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.'
return redirect(url_for('login1'))
else:
return render_template('hello.html', error=error)
if __name__ == '__main__':
APP.debug=True
APP.run()
Upvotes: 3
Reputation: 20369
Try this.
First change the line in app.py
@APP.route('/?', methods=['GET', 'POST'])
to
@APP.route('/login', methods=['GET', 'POST'])
And then in login1.html
, change these lines to corresponding lines.
<form action="/login" method="post">
<input type="text" placeholder="Username" required="" id="username" name="username" />
<input type="password" placeholder="Password" required="" id="password" name="password" />
Update
<form action="/login" method="post">
<h1>Login Form</h1>
<div>
<input type="text" placeholder="Username" required="" id="username" name="username" />
</div>
<div>
<input type="password" placeholder="Password" required="" id="password" name="password" />
</div>
<div>
<input type="submit" value="Log in" />
</div>
</form><!-- form -->
Update 2 hello.html
<html lang='en'>
<head>
<meta charset="utf-8" />
<title>Hello</title>
</head>
{% if error %}
{{ error }}
{% endif %}
<body>
Hello
</body>
</html>
Upvotes: 2