E-commerce Website (Basic)
Abstract
Build a Basic E-commerce Website that displays products, allows adding to a shopping cart, and calculates the total price. The app uses Flask for web development and provides a simple web interface. This project demonstrates web development, routing, and template rendering in Python.
Prerequisites
- Python 3.6 or above
- Text Editor or IDE
- Basic understanding of Python syntax
- Familiarity with Flask for web development
- Knowledge of HTML templates
Getting Started
Creating a new project
- Create a new project folder and name it
ecommerce_website
ecommerce_website
. - Create a new file inside the folder and name it
ecommerce_website.py
ecommerce_website.py
. - Open the project folder in your favorite text editor or IDE.
- Copy the code below and paste it into the
ecommerce_website.py
ecommerce_website.py
file.
Write the code
⚙️ ecommerce_website.py
ecommerce_website.py
"""
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)
ecommerce_website.py
"""
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)
Key Features
- Display a list of products
- Add products to a shopping cart
- Calculate total price
- Web interface using Flask and HTML templates
Explanation
Displaying Products
Products are stored in a list of dictionaries and rendered using Flask templates:
ecommerce_website.py
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},
]
ecommerce_website.py
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},
]
Adding to Cart
Users can add products to the cart, which is stored in a list:
ecommerce_website.py
cart.append(product)
ecommerce_website.py
cart.append(product)
Viewing Cart and Total Price
The cart and total price are displayed using Flask templates:
ecommerce_website.py
total_price = sum(item['price'] for item in cart)
return render_template('cart.html', cart=cart, total_price=total_price)
ecommerce_website.py
total_price = sum(item['price'] for item in cart)
return render_template('cart.html', cart=cart, total_price=total_price)
Running the Application
- Save the file.
- Install Flask:
pip install flask
pip install flask
- Run the application:
python ecommerce_website.py
python ecommerce_website.py
Conclusion
This Basic E-commerce Website project is a great way to learn about web development and routing in Python. You can extend it by adding user authentication, product search, or payment integration.
Was this page helpful?
Let us know how we did