E-commerce Website
Abstract
E-commerce Website is a Python project that simulates an online store with product listings, shopping cart, and checkout functionality. It demonstrates web development, backend logic, and user interaction. This project is ideal for learning about web apps, databases, and business logic.
Prerequisites
- Python 3.6 or above
- Flask (
pip install flask
pip install flask
) - SQLite (built-in)
Before you Start
Install Python and Flask. Prepare a sample product database for testing.
Getting Started
- Create a folder named
ecommerce-website
ecommerce-website
. - Create a file named
ecommerce_website.py
ecommerce_website.py
. - Copy the code below into your file.
⚙️ E-commerce Website
E-commerce Website
"""
E-commerce Website (Basic)
A Python application that simulates a basic e-commerce website.
Features include:
- Displaying a list of products.
- Adding products to a shopping cart.
- Calculating the total price.
"""
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
# Sample product data
products = [
{"id": 1, "name": "Laptop", "price": 800},
{"id": 2, "name": "Smartphone", "price": 500},
{"id": 3, "name": "Headphones", "price": 100},
{"id": 4, "name": "Keyboard", "price": 50},
]
# Shopping cart
cart = []
@app.route('/')
def index():
"""Display the list of products."""
return render_template('index.html', products=products)
@app.route('/add_to_cart/<int:product_id>')
def add_to_cart(product_id):
"""Add a product to the shopping cart."""
product = next((p for p in products if p['id'] == product_id), None)
if product:
cart.append(product)
return redirect(url_for('view_cart'))
@app.route('/cart')
def view_cart():
"""Display the shopping cart and total price."""
total_price = sum(item['price'] for item in cart)
return render_template('cart.html', cart=cart, total_price=total_price)
if __name__ == '__main__':
app.run(debug=True)
E-commerce Website
"""
E-commerce Website (Basic)
A Python application that simulates a basic e-commerce website.
Features include:
- Displaying a list of products.
- Adding products to a shopping cart.
- Calculating the total price.
"""
from flask import Flask, render_template, request, redirect, url_for
app = Flask(__name__)
# Sample product data
products = [
{"id": 1, "name": "Laptop", "price": 800},
{"id": 2, "name": "Smartphone", "price": 500},
{"id": 3, "name": "Headphones", "price": 100},
{"id": 4, "name": "Keyboard", "price": 50},
]
# Shopping cart
cart = []
@app.route('/')
def index():
"""Display the list of products."""
return render_template('index.html', products=products)
@app.route('/add_to_cart/<int:product_id>')
def add_to_cart(product_id):
"""Add a product to the shopping cart."""
product = next((p for p in products if p['id'] == product_id), None)
if product:
cart.append(product)
return redirect(url_for('view_cart'))
@app.route('/cart')
def view_cart():
"""Display the shopping cart and total price."""
total_price = sum(item['price'] for item in cart)
return render_template('cart.html', cart=cart, total_price=total_price)
if __name__ == '__main__':
app.run(debug=True)
- Run the script:
python ecommerce_website.py
python ecommerce_website.py
Explanation
Code Breakdown
- Import required modules.
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
from flask import Flask, render_template, request, redirect, url_for
import sqlite3
- Set up Flask app and database.
app = Flask(__name__)
conn = sqlite3.connect('products.db')
cursor = conn.cursor()
app = Flask(__name__)
conn = sqlite3.connect('products.db')
cursor = conn.cursor()
- Define routes and logic.
@app.route('/')
def home():
# Show product listings
pass
@app.route('/cart')
def cart():
# Show shopping cart
pass
@app.route('/checkout')
def checkout():
# Handle checkout
pass
@app.route('/')
def home():
# Show product listings
pass
@app.route('/cart')
def cart():
# Show shopping cart
pass
@app.route('/checkout')
def checkout():
# Handle checkout
pass
- Run the app.
if __name__ == '__main__':
app.run(debug=True)
if __name__ == '__main__':
app.run(debug=True)
Features
- Product listings
- Shopping cart
- Checkout functionality
- Simple web interface
- Easy to extend for more features
How It Works
- Handles HTTP requests
- Renders templates for pages
- Stores products and cart in database
GUI Components
- Home page: Lists products
- Cart page: Shows selected items
- Checkout page: Handles payment
Use Cases
- Simulate online shopping
- Learn web development
- Build portfolio projects
Next Steps
You can enhance this project by:
- Adding user authentication
- Supporting payment gateways
- Improving UI with CSS
- Adding product search/filter
- Deploying online
Enhanced Version Ideas
def add_payment_gateway():
# Integrate with Stripe or PayPal
pass
def add_user_login():
# Require login for checkout
pass
def add_payment_gateway():
# Integrate with Stripe or PayPal
pass
def add_user_login():
# Require login for checkout
pass
Troubleshooting Tips
- Flask errors: Check installation
- App not running: Check port and debug mode
- Database errors: Check database setup
Conclusion
This project teaches Flask basics, database usage, and web app structure. Extend it for more features and production use.
Was this page helpful?
Let us know how we did