Subscription-Based Content Platform

Project Structure

subscription_platform/
│
├── app.py
├── models.py
├── templates/
│   ├── index.html
│   ├── login.html
│   ├── signup.html
│   ├── dashboard.html
│   └── content.html
└── static/
    └── style.css

1. Set Up Flask and Dependencies

Make sure you have Flask and its dependencies installed. You can install them using pip:

pip install Flask Flask-SQLAlchemy Flask-Login Flask-WTF

2. Create the Models

In models.py, define the user and content models.

from flask_sqlalchemy import SQLAlchemy
from flask_login import UserMixin

db = SQLAlchemy()

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(150), unique=True, nullable=False)
    password = db.Column(db.String(150), nullable=False)
    subscription_active = db.Column(db.Boolean, default=False)

class Content(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(150), nullable=False)
    body = db.Column(db.Text, nullable=False)

3. Set Up Flask Application

In app.py, set up your Flask application and routes.

from flask import Flask, render_template, redirect, url_for, request, flash
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, login_user, login_required, logout_user, current_user
from models import db, User, Content
from werkzeug.security import generate_password_hash, check_password_hash

app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db.init_app(app)
login_manager = LoginManager()
login_manager.init_app(app)

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        hashed_password = generate_password_hash(password, method='sha256')
        new_user = User(username=username, password=hashed_password)
        db.session.add(new_user)
        db.session.commit()
        flash('Signup successful! You can now log in.')
        return redirect(url_for('login'))
    return render_template('signup.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()
        if user and check_password_hash(user.password, password):
            login_user(user)
            return redirect(url_for('dashboard'))
        flash('Login failed. Check your credentials.')
    return render_template('login.html')

@app.route('/dashboard')
@login_required
def dashboard():
    return render_template('dashboard.html', user=current_user)

@app.route('/content/<int:content_id>')
@login_required
def content(content_id):
    content = Content.query.get_or_404(content_id)
    return render_template('content.html', content=content)

@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('index'))

if __name__ == '__main__':
    with app.app_context():
        db.create_all()
    app.run(debug=True)

4. Create the HTML Templates

Create basic templates in the templates folder.

  • index.html:
<h1>Welcome to the Subscription Platform</h1> <a href="{{ url_for('signup') }}">Sign Up</a> <a href="{{ url_for('login') }}">Login</a>
  • login.html:
<form method="POST"> <input type="text" name="username" placeholder="Username" required> <input type="password" name="password" placeholder="Password" required> <button type="submit">Login</button> </form>
  • signup.html:
<form method="POST"> <input type="text" name="username" placeholder="Username" required> <input type="password" name="password" placeholder="Password" required> <button type="submit">Sign Up</button> </form>
  • dashboard.html:
<h1>Hello, {{ user.username }}</h1> <a href="{{ url_for('logout') }}">Logout</a>
  • content.html:
<h1>{{ content.title }}</h1> <p>{{ content.body }}</p> <a href="{{ url_for('dashboard') }}">Back to Dashboard</a>

5. CSS for Basic Styling

You can add a basic CSS file in the static folder to style your application (style.css).

6. Running the Application

Make sure you’re in the project directory, then run the Flask application:

python app.py

Visit http://127.0.0.1:5000/ in your web browser.

Notes

  • Payment Processing: To implement subscriptions, integrate a payment processor like Stripe or PayPal.
  • Content Management: Expand the app to allow content creation and management by users.
  • Security: Consider enhancing security features (e.g., email verification, password recovery).
  • Deployment: Deploy the application using a platform like Heroku, AWS, or DigitalOcean.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *