My Blog

My WordPress Blog

My Blog

My WordPress Blog

E-Learning Platform

Prerequisites

  • Python 3.x
  • Flask
  • SQLite (for simplicity, though you can use any database)
  • HTML/CSS knowledge

Project Structure

arduinoCopy codee_learning/
│
├── app.py
├── templates/
│   ├── base.html
│   ├── index.html
│   ├── login.html
│   ├── register.html
│   └── courses.html
├── static/
│   └── style.css
└── database.db

Step 1: Install Flask

Make sure you have Flask installed:

bashCopy codepip install Flask

Step 2: Create app.py

This file will contain the main application logic.

pythonCopy codefrom flask import Flask, render_template, redirect, url_for, request, session
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash

app = Flask(__name__)
app.secret_key = 'your_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db'
db = SQLAlchemy(app)

# Database model
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(120), nullable=False)
class Course(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(200), nullable=False)
description = db.Column(db.String(500), nullable=False)
db.create_all() # Routes @app.route('/') def index():
return render_template('index.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):
        session['user_id'] = user.id
        return redirect(url_for('courses'))
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST']) def register():
if request.method == 'POST':
    username = request.form['username']
    password = generate_password_hash(request.form['password'], method='sha256')
    new_user = User(username=username, password=password)
    db.session.add(new_user)
    db.session.commit()
    return redirect(url_for('login'))
return render_template('register.html')
@app.route('/courses') def courses():
if 'user_id' not in session:
    return redirect(url_for('login'))
courses = Course.query.all()
return render_template('courses.html', courses=courses)
if __name__ == '__main__':
app.run(debug=True)

Step 3: Create HTML Templates

base.html

htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
&lt;meta charset="UTF-8"&gt;
&lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
&lt;link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}"&gt;
&lt;title&gt;E-Learning Platform&lt;/title&gt;
</head> <body>
&lt;header&gt;
    &lt;h1&gt;E-Learning Platform&lt;/h1&gt;
    &lt;nav&gt;
        &lt;a href="{{ url_for('index') }}"&gt;Home&lt;/a&gt;
        {% if 'user_id' not in session %}
            &lt;a href="{{ url_for('login') }}"&gt;Login&lt;/a&gt;
            &lt;a href="{{ url_for('register') }}"&gt;Register&lt;/a&gt;
        {% else %}
            &lt;a href="{{ url_for('courses') }}"&gt;Courses&lt;/a&gt;
            &lt;a href="{{ url_for('logout') }}"&gt;Logout&lt;/a&gt;
        {% endif %}
    &lt;/nav&gt;
&lt;/header&gt;
&lt;main&gt;
    {% block content %}{% endblock %}
&lt;/main&gt;
</body> </html>

index.html

htmlCopy code{% extends 'base.html' %}
{% block content %}
<h2>Welcome to the E-Learning Platform</h2>
<p>Your journey of learning starts here!</p>
{% endblock %}

login.html

htmlCopy code{% extends 'base.html' %}
{% block content %}
<h2>Login</h2>
<form method="POST">
&lt;input type="text" name="username" placeholder="Username" required&gt;
&lt;input type="password" name="password" placeholder="Password" required&gt;
&lt;button type="submit"&gt;Login&lt;/button&gt;
</form> {% endblock %}

register.html

htmlCopy code{% extends 'base.html' %}
{% block content %}
<h2>Register</h2>
<form method="POST">
&lt;input type="text" name="username" placeholder="Username" required&gt;
&lt;input type="password" name="password" placeholder="Password" required&gt;
&lt;button type="submit"&gt;Register&lt;/button&gt;
</form> {% endblock %}

courses.html

htmlCopy code{% extends 'base.html' %}
{% block content %}
<h2>Available Courses</h2>
<ul>
{% for course in courses %}
    &lt;li&gt;{{ course.title }}: {{ course.description }}&lt;/li&gt;
{% endfor %}
</ul> {% endblock %}

Step 4: Create CSS File

style.css

cssCopy codebody {
font-family: Arial, sans-serif;
} header {
background: #333;
color: #fff;
padding: 10px 0;
} nav a {
color: #fff;
margin: 0 15px;
text-decoration: none;
} h2 {
color: #333;
} form {
display: flex;
flex-direction: column;
max-width: 300px;
}

Step 5: Run the Application

  1. Initialize the database:bashCopy codepython Then run:pythonCopy codefrom app import db db.create_all() exit()
  2. Start the Flask app:bashCopy codepython app.py
  3. Open your browser and go to http://127.0.0.1:5000.
E-Learning Platform

Leave a Reply

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

Scroll to top