Flask E-commerce Development
Flask E-commerce Development
To build a modern e-commerce site for home appliances and tools using Flask, you'll need:
Tech Stack
Backend: Flask (with Flask-RESTful or Flask-SQLAlchemy)
Frontend: Jinja2 (Flask templates) or a modern frontend framework like React or Vue.js
Deployment: Docker, Gunicorn, Nginx, and a cloud provider (AWS, DigitalOcean, or self-
hosted)
Core Features
1. User Authentication
2. Product Management
4. Admin Dashboard
1/11
5. User Features
Would you like a base template with Flask boilerplate to get started?
This is your base Flask project setup with SQLAlchemy and Flask-Login for authentication.
Next steps:
advise the best folder & files structure for the project
A well-structured Flask project for an e-commerce site should follow the Blueprints pattern
for scalability. Here’s an ideal folder structure:
2/11
bash
/flask_ecommerce
│── /app
│ │── /static # CSS, JS, Images
│ │── /templates # Jinja2 templates
│ │── /models # Database models
│ │ │── __init__.py
│ │ │── user.py
│ │ │── product.py
│ │── /routes # Views and API routes
│ │ │── __init__.py
│ │ │── auth.py
│ │ │── product.py
│ │── /forms # WTForms for user authentication and checkout
│ │ │── __init__.py
│ │ │── login.py
│ │ │── register.py
│ │── __init__.py # Initializes Flask app
│── /migrations # Alembic migrations for database changes
│── config.py # Configuration settings
│── run.py # Entry point to start the app
│── requirements.txt # Dependencies
│── README.md # Project documentation
Key Features
models/ : Contains database models for users, products, orders, etc.
routes/ : Contains separate files for authentication, product views, and other features.
Would you like me to update your boilerplate to align with this structure?
3/11
import os
# Initialize database
db = SQLAlchemy(app)
# Import Blueprints
from app.routes.auth import auth_bp
from app.routes.product import product_bp
# Register Blueprints
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(product_bp, url_prefix='/products')
I've updated your boilerplate to follow the structured approach using Blueprints for
authentication and product management. You can now create auth.py and product.py
inside app/routes/ and define your authentication and product-related routes there. Let me
know if you need help with those files!
You can run the following command in your terminal to create the folder structure in one go:
bash
4/11
flask_ecommerce/app/{__init__.py,models/__init__.py,models/user.py,models/product.py,ro
Explanation:
mkdir -p creates nested directories if they don’t exist.
Run this command inside your project directory, and it will set up the structure instantly. Let
me know if you need further refinements! 🚀
ok how about the code you create what file name to save
The code you have should be saved as run.py in the root directory of your project
( flask_ecommerce/ ).
It serves as the entry point for your Flask application. When you run:
bash
python run.py
Would you like me to generate code for auth.py and product.py next?
To install all the necessary dependencies for your Flask e-commerce project, create a
requirements.txt file in the project root ( flask_ecommerce/ ) and add the following:
requirements.txt
nginx
Flask
Flask-SQLAlchemy
Flask-Login
Flask-WTF
Flask-Migrate
Flask-Bcrypt
Flask-RESTful
5/11
python-dotenv
gunicorn
bash
Explanation of Packages:
Flask : Core framework.
bash
flask --version
Would you like help setting up Flask-Migrate for managing database migrations?
6/11
(/home/web/projects/flask_ecommerce/app/routes/auth.py
)
The error "ImportError: cannot import name 'auth_bp' from 'app.routes.auth'" means that
auth_bp is either not defined in app/routes/auth.py or the file itself is missing.
Steps to Fix:
1. Ensure app/routes/auth.py exists.
app/routes/auth.py
python
@auth_bp.route('/login')
def login():
return "Login Page"
@auth_bp.route('/register')
def register():
return "Register Page"
app/routes/product.py
python
@product_bp.route('/')
def product_home():
return "Product Home Page"
bash
7/11
python3 run.py
This should fix the issue. Let me know if you need further debugging! 🚀
i got the error : python3 run.py
Traceback (most recent call last):
File "/home/web/projects/flask_ecommerce/run.py", line
28, in <module>
@app.before_first_request
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Flask' object has no attribute
'before_first_request'. Did you mean: '_got_first_request'?
(venv) web@fd7d32b71a44:~/projects/flask_ecommerce$
The error is due to before_first_request being removed in Flask 2.3+. You should replace it
with an application context startup event using with app.app_context() .
Fix
Replace:
python
@app.before_first_request
def create_tables():
db.create_all()
With:
python
with app.app_context():
db.create_all()
Updated Code
Here’s how your run.py should look after fixing the issue:
python
8/11
from flask_login import LoginManager
import os
# Initialize database
db = SQLAlchemy(app)
# Import Blueprints
from app.routes.auth import auth_bp
from app.routes.product import product_bp
# Register Blueprints
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(product_bp, url_prefix='/products')
This should resolve the error. Let me know if you need further assistance! 🚀
site run http://127.0.0.1:5000 , but it give error : Not Found
The requested URL was not found on the server. If you
entered the URL manually please check your spelling and
try again.
The "Not Found" error usually means there are no routes defined for the homepage ( / ).
Since you've set up Blueprints for authentication ( /auth ) and products ( /products ), the
root URL ( / ) needs a route.
9/11
Fix
Add a home route inside run.py :
python
@app.route('/')
def home():
return "Welcome to the E-commerce site!"
Alternatively
You can move the home route into a new Blueprint in app/routes/main.py :
1. Create app/routes/main.py
python
@main_bp.route('/')
def home():
return "Welcome to the E-commerce site!"
python
# Register Blueprints
app.register_blueprint(main_bp) # Now handles "/"
app.register_blueprint(auth_bp, url_prefix='/auth')
app.register_blueprint(product_bp, url_prefix='/products')
Restart Flask
After making these changes, restart the Flask app:
10/11
bash
python3 run.py
2/2
11/11