Flask Python - 请求的URL在服务器上未找到,以前可以,现在却不行
我刚在新电脑上克隆了一个我正在做的项目,因为我的旧电脑坏了。我安装了所有的包,但当我尝试访问网站时却出现了登录错误。上传时一切都和之前一样,唯一不同的是我需要重新安装这些包。我不确定这是否可能是因为在上传时有用户已经登录了。
from flask import Flask, render_template, request, redirect, url_for, flash, abort
from flask_login import LoginManager, login_user, logout_user, UserMixin, user_logged_in, login_required, current_user, user_logged_out
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import desc, asc
from werkzeug import security as pswd_controller
from werkzeug.utils import secure_filename
from functools import wraps
import calendar
import smtplib
import shutil
import os
db = SQLAlchemy()
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///product_info.db"
app.secret_key = b'test_key'
db.init_app(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
@login_manager.user_loader
def load_user(user_id):
return db.get_or_404(Users, user_id)
class Users(db.Model, UserMixin):
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key=True, unique=True)
name = db.Column(db.String, nullable=False)
email = db.Column(db.String, unique=True, nullable=False)
date_of_birth = db.Column(db.String, nullable=False)
password = db.Column(db.String, nullable=False)
favorited_items = db.relationship('Favorite', backref="client")
carted_items = db.relationship('Cart', backref="client")
delivery_address = db.relationship('Address', backref="client")
# author = db.relationship('BlogPost', backref="author") # For Parent
# author_id = db.Column(db.Integer, db.ForeignKey('users.id')) # For child
class ProductInfo(db.Model):
__tablename__ = "Product Info"
id = db.Column(db.Integer, primary_key=True, unique=True)
product_name = db.Column(db.String, unique=True, nullable=False)
type = db.Column(db.String, nullable=False)
cost = db.Column(db.Integer, nullable=False)
description = db.Column(db.String, nullable=False)
reviewer = db.relationship('Reviews', backref="poster")
class Reviews(db.Model):
__tablename__ = "Reviews"
id = db.Column(db.Integer, primary_key=True, unique=True)
client_id = db.Column(db.Integer, db.ForeignKey('Users.id'), nullable=False)
product_id = db.Column(db.Integer, db.ForeignKey('Product Info.id'), nullable=False)
review = db.Column(db.String, nullable=False)
class Favorite(db.Model):
__tablename__ = "Favorites"
id = db.Column(db.Integer, primary_key=True, unique=True)
client_id = db.Column(db.Integer, db.ForeignKey('Users.id'), nullable=False)
item_id = db.Column(db.Integer, nullable=False)
class Cart(db.Model):
__tablename__ = "Carted Items"
id = db.Column(db.Integer, primary_key=True, unique=True)
client_id = db.Column(db.Integer, db.ForeignKey('Users.id'), nullable=False)
item_id = db.Column(db.Integer, nullable=False)
product_amount = db.Column(db.Integer, nullable=False)
class Address(db.Model):
__tablename__ = "Addresses"
id = db.Column(db.Integer, primary_key=True, unique=True)
client_id = db.Column(db.Integer, db.ForeignKey('Users.id'), nullable=False)
address = db.Column(db.Integer, nullable=False)
checked = db.Column(db.Boolean, nullable=False)
with app.app_context():
db.create_all()
def admin_only(func):
@wraps(func)
def decorated_function(*args, **kwargs):
if current_user.id != 1:
return redirect(abort(403))
return func(*args, **kwargs)
return decorated_function
@app.route('/', methods=['GET', 'POST'])
def homepage():
products = db.session.execute(db.select(ProductInfo).order_by(ProductInfo.id)).scalars()
product_image_path = "/static/Images/Product Images"
if request.method == 'POST':
if 'delete-button' in request.form:
product_image_path = (f"C:/Users/User1/Documents/Python_Projects/"
f"Ragdall/static/Images/Product Images")
refactor_database(product_image_path)
return redirect(url_for('homepage'))
elif 'by-id' in request.form:
products = db.session.execute(db.select(ProductInfo).order_by(desc(ProductInfo.id))).scalars()
return redirect(url_for('homepage'))
elif 'low-to-high' in request.form:
products = db.session.execute(db.select(ProductInfo).order_by(asc(ProductInfo.cost))).scalars()
return render_template('products.html', products=products, product_image_path=product_image_path)
elif 'high-to-low' in request.form:
products = db.session.execute(db.select(ProductInfo).order_by(desc(ProductInfo.cost))).scalars()
return render_template('products.html', products=products, product_image_path=product_image_path)
elif request.method == "GET":
return render_template('products.html', products=products, product_image_path=product_image_path)
我希望能看到一个像之前那样完全正常的网站。你觉得问题可能出在哪里?我确实有一个products.html。
1 个回答
0
我需要清除浏览器的缓存和 cookies,然后再运行应用程序。我觉得有些浏览器会保存你 Flask 应用程序的旧数据,所以你必须清理缓存才能让一切正常运行。