Untitled
unknown
plain_text
a year ago
2.2 kB
1
Indexable
Never
# Import necessary modules from flask import Flask, render_template, request, redirect, url_for, flash from flask_sqlalchemy import SQLAlchemy # Create the Flask app app = Flask(__style spot by aditya__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///ecommerce.db' app.secret_key = 'your_secret_key' # Create a database instance db = SQLAlchemy(app) # Define the Product model class Product(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) description = db.Column(db.String(200)) price = db.Column(db.Float, nullable=False) stock = db.Column(db.Integer, nullable=False) # Create the database db.create_all() # Route to display the list of products @app.route('/') def index(): products = Product.query.all() return render_template('index.html', products=products) # Route to view a product's details @app.route('/product/<int:id>') def product_detail(id): product = Product.query.get(id) return render_template('product_detail.html', product=product) # Sample route for adding a product (admin-only, authentication not implemented) @app.route('/add_product', methods=['GET', 'POST']) def add_product(): if request.method == 'POST': name = request.form['name'] description = request.form['description'] price = request.form['price'] stock = request.form['stock'] product = Product(name=name, description=description, price=price, stock=stock) db.session.add(product) db.session.commit() flash('Product added successfully!', 'success') return redirect(url_for('index')) return render_template('add_product.html') # Sample route for removing a product (admin-only, authentication not implemented) @app.route('/delete_product/<int:id>', methods=['POST']) def delete_product(id): product = Product.query.get(id) db.session.delete(product) db.session.commit() flash('Product deleted successfully!', 'success') return redirect(url_for('index')) if __name__ == '__main__': app.run(debug=True)