2015-04-05 65 views
1

我寫的這個論壇,我想驗證用戶名和密碼,但我有result.Method不允許該方法不適用於請求的URL。在瓶

hello.html的

<html lang='en'> 
 
<head> 
 
    <meta charset="utf-8" /> 
 
    <title>Hello</title> 
 

 
</head> 
 
{% if error %} 
 
    {{ error }} 
 
{% endif %} 
 
<body> 
 

 
     Hello 
 

 
</body> 
 
</html>

的方法是不允許的爲請求的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> 

我寫了一個論壇:

在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() 

我無法驗證輸入? 如何能幫我請

+0

嘗試將@app.route('/?',methods = ['GET','POST'])'改爲'@ APP.route('/ login',methods = ['GET', 'POST'])'.and in html,'form action =「/ login」' – itzMEonTV 2015-04-05 20:08:45

+1

我有同樣的結果 – 2015-04-05 20:10:31

+0

你在'app.py'和'html'中都改過了嗎? – itzMEonTV 2015-04-05 20:14:01

回答

2

我修改你的代碼app.py代碼到下方,與您當前hello.html的和login1沿.html這工作正常。

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() 
0

試試這個。

首先變化app.py

@APP.route('/?', methods=['GET', 'POST']) 

行至

@APP.route('/login', methods=['GET', 'POST']) 

然後在login1.html,改變這些線對應的線。

<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" /> 

更新

<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 --> 

更新2hello.html

<html lang='en'> 
<head> 
    <meta charset="utf-8" /> 
    <title>Hello</title> 

</head> 
{% if error %} 
    {{ error }} 
{% endif %} 
<body> 

     Hello 

</body> 
</html> 
+0

我改變了,但我有同樣的結果:錯誤的請求 瀏覽器(或代理)發送請求,這個服務器無法理解。 – 2015-04-05 20:34:06

+0

哪個錯誤?該方法不允許用於請求的URL? – itzMEonTV 2015-04-05 20:35:38

+0

錯誤的請求瀏覽器(或代理服務器)發送了一個請求,表明此服務器無法理解@itzmeontv – 2015-04-05 20:40:40

相關問題