Step 1: Set Up the Environment
- Install Flask: You can install Flask and other dependencies using pip. Create a virtual environment first for better management.
mkdir custom_crm cd custom_crm python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate` pip install Flask Flask-SQLAlchemy
- Project Structure: Your project structure should look something like this:
custom_crm/ ├── app.py ├── models.py ├── templates/ │ ├── index.html │ ├── add_customer.html └── venv/
Step 2: Create the Models
In models.py
, define the database models. For simplicity, we’ll create a Customer
model.
pythonCopy code# models.py
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class Customer(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
phone = db.Column(db.String(15))
notes = db.Column(db.Text)
def __repr__(self):
return f'<Customer {self.name}>'
Step 3: Create the Flask Application
In app.py
, set up the Flask app and routes.
# app.py
from flask import Flask, render_template, request, redirect, url_for
from models import db, Customer
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///crm.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.init_app(app)
with app.app_context():
db.create_all()
@app.route('/')
def index():
customers = Customer.query.all()
return render_template('index.html', customers=customers)
@app.route('/add', methods=['GET', 'POST'])
def add_customer():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
phone = request.form['phone']
notes = request.form['notes']
new_customer = Customer(name=name, email=email, phone=phone, notes=notes)
db.session.add(new_customer)
db.session.commit()
return redirect(url_for('index'))
return render_template('add_customer.html')
if __name__ == '__main__':
app.run(debug=True)
Step 4: Create the HTML Templates
Create a simple HTML template for the customer list and the form to add new customers.
templates/index.html
:
<!DOCTYPE html>
<html>
<head>
<title>CRM System</title>
</head>
<body>
<h1>Customer List</h1>
<a href="/add">Add Customer</a>
<ul>
{% for customer in customers %}
<li>{{ customer.name }} - {{ customer.email }}</li>
{% endfor %}
</ul>
</body>
</html>
templates/add_customer.html
:
<!DOCTYPE html>
<html>
<head>
<title>Add Customer</title>
</head>
<body>
<h1>Add New Customer</h1>
<form method="POST">
Name: <input type="text" name="name" required><br>
Email: <input type="email" name="email" required><br>
Phone: <input type="text" name="phone"><br>
Notes: <textarea name="notes"></textarea><br>
<input type="submit" value="Add Customer">
</form>
</body>
</html>
Step 5: Run the Application
- Initialize the Database: If you haven’t created the database yet, run the application once to initialize it.
- Run the Flask App:bashCopy code
python app.py
- Access the CRM: Open your browser and go to
http://127.0.0.1:5000/
to see your CRM in action!
Leave a Reply