BLOG CODE

BLOG Features of This Code ✅ Create a new blog post ✅ View all posts ✅ Read a single post ✅ Edit a post ✅ Delete a post ✅ Uses Flask-WTF for form handling ✅ Uses Bootstrap for a clean UI 1️⃣ Install Dependencies Run this in your terminal: pip install flask flask_sqlalchemy flask_wtf wtforms CODE from flask import Flask, render_template, redirect, url_for, request, flash from flask_sqlalchemy import SQLAlchemy from flask_wtf import FlaskForm from wtforms import StringField, TextAreaField, SubmitField from wtforms.validators import DataRequired app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db' app.config['SECRET_KEY'] = 'your_secret_key' db = SQLAlchemy(app) # Database Model for Blog Post class BlogPost(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) # Form for Creating & Editing Posts class PostForm(FlaskForm): title = StringField("Title", validators=[DataRequired()]) content = TextAreaField("Content", validators=[DataRequired()]) submit = SubmitField("Submit") # Home Page - Show All Posts @app.route('/') def index(): posts = BlogPost.query.order_by(BlogPost.id.desc()).all() return render_template('index.html', posts=posts) # Create New Post @app.route('/new', methods=['GET', 'POST']) def new_post(): form = PostForm() if form.validate_on_submit(): new_post = BlogPost(title=form.title.data, content=form.content.data) db.session.add(new_post) db.session.commit() flash("Post created successfully!", "success") return redirect(url_for('index')) return render_template('post_form.html', form=form, action="New Post") # View Single Post @app.route('/post/') def post_detail(post_id): post = BlogPost.query.get_or_404(post_id) return render_template('post_detail.html', post=post) # Edit Post @app.route('/edit/', methods=['GET', 'POST']) def edit_post(post_id): post = BlogPost.query.get_or_404(post_id) form = PostForm(obj=post) if form.validate_on_submit(): post.title = form.title.data post.content = form.content.data db.session.commit() flash("Post updated successfully!", "success") return redirect(url_for('index')) return render_template('post_form.html', form=form, action="Edit Post") # Delete Post @app.route('/delete/') def delete_post(post_id): post = BlogPost.query.get_or_404(post_id) db.session.delete(post) db.session.commit() flash("Post deleted successfully!", "danger") return redirect(url_for('index')) if __name__ == '__main__': with app.app_context(): db.create_all() # Creates database if not exists app.run(debug=True) 3️⃣ Templates (Inside templates/ folder) index.html (Homepage - Show All Posts) Blog

My Blog

New Post {% for post in posts %}

{{ post.title }}

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

Edit Delete
{% endfor %} post_form.html (Create & Edit Post) {{ action }}

{{ action }}

{{ form.hidden_tag() }}
{{ form.title.label(class="form-label") }} {{ form.title(class="form-control") }}
{{ form.content.label(class="form-label") }} {{ form.content(class="form-control", rows=5) }}
Cancel
post_detail.html (View Post) {{ post.title }}

{{ post.title }}

{{ post.content }}

Edit Delete Back 4️⃣ Running the Blog Install dependencies pip install flask flask_sqlalchemy flask_wtf wtforms Run the script python app.py Open in browser http://127.0.0.1:5000/ Key Improvements in This Version 🔹 Uses Flask-WTF to handle form validation 🔹 Displays flash messages for user actions 🔹 Uses Bootstrap for better UI 🔹 Optimized database queries This setup is clean, efficient, and scalable. Let me know if you need any extra features like user authentication or image uploads!

Comments

Popular posts from this blog

caculator using Applet