我不明白出了什么问题,我让程序下次继续工作,这不仅仅是给我一个内部服务器错误,我尝试自己修复问题,我尝试使用 chatgpt 从第一眼看它说我的代码应该正常工作,然后当我说这个问题,它只是开始大喊大叫,做这个做那个,现在什么都没有真正起作用我打开了应用程序,它正在工作,但是当我尝试通过网址导航时,它说找不到网址,但现在它不再工作了,它只是给了我内部服务器错误 python create.py:
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
import hashlib
app = Flask(__name__)
def init_db():
with sqlite3.connect("user_login_data.db") as connection:
cursor = connection.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
username TEXT PRIMARY KEY,
password TEXT NOT NULL
)
""")
connection.commit()
init_db()
@app.route('/sign-up', methods=['GET', 'POST'])
def sign_up():
error = None
if request.method == 'POST':
username = request.form.get("username")
password = request.form.get("password")
confirm_password = request.form.get("confirm_password")
if password != confirm_password:
error = "Passwords do not match."
else:
hashed_password = hashlib.sha1(password.encode()).hexdigest()
try:
with sqlite3.connect("user_login_data.db") as connection:
cursor = connection.cursor()
cursor.execute(
"INSERT INTO users (username, password) VALUES (?, ?)",
(username, hashed_password)
)
connection.commit()
return redirect(url_for('home'))
except sqlite3.IntegrityError:
error = "Username already exists. Please try another."
return render_template("signup.html")
@app.route('/index')
def index():
return render_template("index.html")
python app.py:
from flask import Flask, render_template, request, redirect, url_for
import hashlib
import sqlite3
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
username = request.form.get("username")
password = request.form.get("password")
hashed_password = hashlib.sha1(password.encode()).hexdigest()
connection = sqlite3.connect('user_login_data.db')
cursor = connection.cursor()
query = "SELECT username, password FROM users WHERE username = ? AND password = ?"
cursor.execute(query, (username, hashed_password))
result = cursor.fetchall()
if len(result) == 0:
error = "Username or password is incorrect."
return render_template("index.html")
else:
return render_template("home_page.html")
return render_template("index.html")
@app.route('/signup')
def signup():
return render_template("signup.html")
signup.html:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/static/styles.css">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
<title>Login</title>
</head>
<body>
<center>
<div class="main">
<h1 class = "sign" >Create an Account</h1>
<form method="POST" class = "form">
<br><br>
<label for="username"></label>
<input class = "un" type="text" name="username" placeholder="Enter your username here " required>
<br>
<label for="username"></label>
<input class = "pass" type="password" name="password" placeholder="Enter your password here" required>
<br>
<label for="username"></label>
<input class = "pass" type="password" name="confirm_password" placeholder="Re-enter password here" required>
<br><br><br>
<button class = "submit" type="submit">Create Account</button>
<br><br><br>
<p class="forgot" align="center"> have an account? ? <a href="{{ url_for('index') }}"> Sign in</a> </p>
</form>
</div>
</center>
</body>
</html>
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="/static/style.css">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
<title>Login</title>
</head>
<body>
<center>
<div class = "main">
<h1 class = "sign"> Sign in </h1>
<form method="POST" class = "form1" >
<br>
<input class = "un" type="text" name="username" align="center" placeholder="Username" required>
<input class= "pass" type="password" name="password" align="center" placeholder="Password" minlength="8" required />
<input class = "submit" type="submit" value="Sign in" />
<br><br>
<p class="forgot" align="center"> Don't have an account? ? <a href="{{ url_for('signup')}}"> Sign up</a> </p>
</form>
</div>
</center>
</body>
</html>
当我第一次下载 python 时,vs code 无法识别它,我尝试了不同的版本,但没有任何效果,然后我找到了解决方案,即更改路径。最后python被识别了,但是flask不是一直说导入“flask”无法解析Pylance(reportMissingImports)[Ln 1,col 6]但是当我在终端中编写flask run时,开发服务器就会出现,但我发现按 CRTL+SHFT+P 然后编写 python:selectterpreter 然后选择 python 3.12.7 解决此固定烧瓶无法识别的问题。
当我现在运行代码时,它只会给出以下输出:
PS C:\Users\nsbou\OneDrive\Desktop\web project> flask run
* Debug mode: off
WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:5000
Press CTRL+C to quit
[2024-11-22 19:45:49,515] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\flask\app.py", line 1511, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\flask\app.py", line 919, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\flask\app.py", line 917, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\flask\app.py", line 902, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\OneDrive\Desktop\web project\app.py", line 32, in login
return render_template("index.html")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\flask\templating.py", line 150, in render_template
return _render(app, template, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\flask\templating.py", line 131, in _render
rv = template.render(context)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\jinja2\environment.py", line 1304, in render
self.environment.handle_exception()
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\jinja2\environment.py", line 939, in handle_exception
raise rewrite_traceback_stack(source=source)
File "C:\Users\nsbou\OneDrive\Desktop\web project\templates\index.html", line 25, in top-level template code
<p class="forgot" align="center"> Don't have an account?<a href="{{ url_for('sign_up') }}"> Sign up</a>
^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\flask\app.py", line 1121, in url_for
return self.handle_url_build_error(error, endpoint, values)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\flask\app.py", line 1110, in url_for
rv = url_adapter.build( # type: ignore[union-attr]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\nsbou\AppData\Local\Programs\Python\Python312\Lib\site-packages\werkzeug\routing\map.py", line 924, in build
raise BuildError(endpoint, values, method, self)
werkzeug.routing.exceptions.BuildError: Could not build url for endpoint 'sign_up'. Did you mean 'signup' instead?
127.0.0.1 - - [22/Nov/2024 19:45:49] "GET / HTTP/1.1" 500 -
端点名称和函数名称都很棒。
@app.route('/sign-up', ... )
def sign_up():
诊断表明您的网络浏览器已导航 到
/sign_up
,当 /sign-up
是有意的。
有点令人困惑,你还有一个完美的
/signup
端点
提供静态内容。考虑将其名称稍微长一些,
以避免潜在的混乱。