├── README.md ├── add.html ├── app.py ├── base.html ├── edit.html ├── index.html ├── post.html ├── requirements.txt └── styles.css /README.md: -------------------------------------------------------------------------------- 1 | # Blog-Platform 2 | A simple blog platform where users can create, edit, and delete posts. 3 | 4 | # Technologies 5 | Python, Flask, SQLAlchemy, HTML, CSS 6 | -------------------------------------------------------------------------------- /add.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block title %}Add Post{% endblock %} 4 | 5 | {% block content %} 6 |

Add Post

7 |
8 | 9 | 10 | 11 | 12 | 13 |
14 | {% endblock %} 15 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, render_template, request, redirect, url_for 2 | from flask_sqlalchemy import SQLAlchemy 3 | 4 | app = Flask(__name__) 5 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db' 6 | db = SQLAlchemy(app) 7 | 8 | class Post(db.Model): 9 | id = db.Column(db.Integer, primary_key=True) 10 | title = db.Column(db.String(100), nullable=False) 11 | content = db.Column(db.Text, nullable=False) 12 | 13 | @app.route('/') 14 | def index(): 15 | posts = Post.query.all() 16 | return render_template('index.html', posts=posts) 17 | 18 | @app.route('/post/') 19 | def post(post_id): 20 | post = Post.query.get_or_404(post_id) 21 | return render_template('post.html', post=post) 22 | 23 | @app.route('/add', methods=['GET', 'POST']) 24 | def add(): 25 | if request.method == 'POST': 26 | title = request.form['title'] 27 | content = request.form['content'] 28 | new_post = Post(title=title, content=content) 29 | db.session.add(new_post) 30 | db.session.commit() 31 | return redirect(url_for('index')) 32 | return render_template('add.html') 33 | 34 | @app.route('/edit/', methods=['GET', 'POST']) 35 | def edit(post_id): 36 | post = Post.query.get_or_404(post_id) 37 | if request.method == 'POST': 38 | post.title = request.form['title'] 39 | post.content = request.form['content'] 40 | db.session.commit() 41 | return redirect(url_for('index')) 42 | return render_template('edit.html', post=post) 43 | 44 | @app.route('/delete/') 45 | def delete(post_id): 46 | post = Post.query.get_or_404(post_id) 47 | db.session.delete(post) 48 | db.session.commit() 49 | return redirect(url_for('index')) 50 | 51 | if __name__ == '__main__': 52 | db.create_all() 53 | app.run(debug=True) 54 | -------------------------------------------------------------------------------- /base.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | {% block title %}{% endblock %} 7 | 8 | 9 | 10 |
11 |

My Blog

12 | 18 |
19 |
20 | {% block content %}{% endblock %} 21 |
22 |
23 |

© 2024 My Blog

24 |
25 | 26 | 27 | -------------------------------------------------------------------------------- /edit.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block title %}Edit Post{% endblock %} 4 | 5 | {% block content %} 6 |

Edit Post

7 |
8 | 9 | 10 | 11 | 12 | 13 |
14 | {% endblock %} 15 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block title %}Home{% endblock %} 4 | 5 | {% block content %} 6 |

Posts

7 | {% for post in posts %} 8 |
9 |

{{ post.title }}

10 |

{{ post.content[:100] }}...

11 | Edit 12 | Delete 13 |
14 | {% endfor %} 15 | {% endblock %} 16 | -------------------------------------------------------------------------------- /post.html: -------------------------------------------------------------------------------- 1 | {% extends 'base.html' %} 2 | 3 | {% block title %}{{ post.title }}{% endblock %} 4 | 5 | {% block content %} 6 |

{{ post.title }}

7 |

{{ post.content }}

8 | {% endblock %} 9 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==2.0.1 2 | Flask-SQLAlchemy==2.5.1 3 | -------------------------------------------------------------------------------- /styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-family: Arial, sans-serif; 3 | margin: 0; 4 | padding: 0; 5 | } 6 | 7 | header { 8 | background-color: #333; 9 | color: white; 10 | padding: 10px 0; 11 | text-align: center; 12 | } 13 | 14 | nav ul { 15 | list-style: none; 16 | padding: 0; 17 | } 18 | 19 | nav ul li { 20 | display: inline; 21 | margin: 0 10px; 22 | } 23 | 24 | nav ul li a { 25 | color: white; 26 | text-decoration: none; 27 | } 28 | 29 | main { 30 | padding: 20px; 31 | } 32 | 33 | .post { 34 | margin-bottom: 20px; 35 | padding: 10px; 36 | border: 1px solid #ddd; 37 | border-radius: 5px; 38 | } 39 | 40 | footer { 41 | background-color: #333; 42 | color: white; 43 | text-align: center; 44 | padding: 10px 0; 45 | position: fixed; 46 | width: 100%; 47 | bottom: 0; 48 | } 49 | --------------------------------------------------------------------------------