werkzeug.routing.exceptions.BuildError:无法为端点 python Flask 构建 url

问题描述 投票:0回答:1

我正在尝试在 Python / Flask 中为个人项目创建登录页面和注册页面,但当我尝试访问注册端点时,我在 vscode 终端中遇到此错误:

“werkzeug.routing.exceptions.BuildError:无法为端点“注册”构建 url。您的意思是“注册”吗?

在代码中,只有 hello 页面可以工作,如果我在端点中包含 /register 或 /login ,它会抛出:

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.

应用程序.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)

登录.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>

注册.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>
routes endpoint internals
1个回答
0
投票

您需要在“Register.html”中更改以下内容: {{url_for('注册')}} 对此 {{url_for('注册')}}

将函数定义中的命名与该函数的调用相匹配。

或者将py代码中的名称更改为大写:

@app.route('/register',methods=['POST','GET']) def R注册():

© www.soinside.com 2019 - 2024. All rights reserved.