admin管理员组文章数量:1122832
I cant understand what's going wrong I leave the program working the next time it's not it just gives me an internal server error I try fixing things my self I try using chatgpt from the first look it says that my code should work properly and then when I say the problem it just start yapping about do this do that and nothing really worked now I opened the application it was working but when I try navigating thru the url it says url not found but now its not working no more it just gives me internal server error 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
)
""")
connectionmit()
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)
)
connectionmit()
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="; 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="; rel="stylesheet">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" <link href=".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>
when I first downloaded python it wasn't getting recognized by vs code I've tried different versions but nothing worked then I found the solution which is was by changing the path. finally python was getting recognized but flask wasn't it kept saying import "flask" could not be resolved Pylance(reportMissingImports)[Ln 1,col 6] but when ever I write flask run in terminal the development server show up but then Ive found the solution by pressing CRTL+SHFT+P then writing python:select interpreter then selecting python 3.12.7 this fixed flask being not recognized.
when ever I run the code now it just gives an output of this :
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 -
I cant understand what's going wrong I leave the program working the next time it's not it just gives me an internal server error I try fixing things my self I try using chatgpt from the first look it says that my code should work properly and then when I say the problem it just start yapping about do this do that and nothing really worked now I opened the application it was working but when I try navigating thru the url it says url not found but now its not working no more it just gives me internal server error 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>
when I first downloaded python it wasn't getting recognized by vs code I've tried different versions but nothing worked then I found the solution which is was by changing the path. finally python was getting recognized but flask wasn't it kept saying import "flask" could not be resolved Pylance(reportMissingImports)[Ln 1,col 6] but when ever I write flask run in terminal the development server show up but then Ive found the solution by pressing CRTL+SHFT+P then writing python:select interpreter then selecting python 3.12.7 this fixed flask being not recognized.
when ever I run the code now it just gives an output of this :
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 -
Share
Improve this question
edited Nov 23, 2024 at 10:02
Anas Bousrih
asked Nov 23, 2024 at 0:56
Anas BousrihAnas Bousrih
112 bronze badges
1 Answer
Reset to default 0The endpoint name, and function name, are great.
@app.route('/sign-up', ... )
def sign_up():
The diagnostic suggests that your web browser navigated
to /sign_up
, when /sign-up
was intended.
Somewhat confusingly, you also have a perfectly nice /signup
endpoint
which serves static content. Consider making its name slightly longer,
to avoid potential confusion.
本文标签: pythonFlask program runs inconsistently and doesn39t redirect pages correctlyStack Overflow
版权声明:本文标题:python - Flask program runs inconsistently and doesn't redirect pages correctly - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736300190a1930728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论