Flask Python- The requested URL was not found on the server. It worked before but now I get

35 Views Asked by At

I just clone a project I am working on a new pc because my old one got damaged. I installed all the packages but I am getting the login error when I try to access the site. Everything is the same when I uploaded it except for the fact that I had to install the packages over. I am not sure if it maybe due to a user being logged in at the time of upload or not.

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)

I expect to see a fully functional website like I did before. What do you think the issue might be? I do have a products.html.

1

There are 1 best solutions below

0
IAMDEMEL On

I needed to clear my browser's cache and cookies and then run the application. I think some browsers do store you Flask application old data so you have to clear the cache to have everything running.