A full-featured blog web application built with Flask, allowing users to register, log in, create and manage posts, update their profiles, and more. This project demonstrates core concepts of Flask, including blueprints, authentication, database models, forms, and template rendering.
- Features
- Project Structure
- Setup & Installation
- Database Models
- Forms
- Routes & Functionality
- Templates
- Static Files
- Migrations
- How It Works
- Possible Improvements
- License
- User registration and login (with hashed passwords)
- User authentication and session management
- Create, update, and delete blog posts
- View all posts and individual post pages
- Update user profile and profile picture
- Flash messages for user feedback
- Responsive design with custom CSS
- Database migrations with Flask-Migrate
Your_Blog/
│
├── flaskblog/
│ ├── __init__.py # App factory, extension initialization
│ ├── models.py # SQLAlchemy models (User, Post)
│ ├── forms.py # WTForms (registration, login, post, account)
│ ├── routes.py # All route handlers (views)
│ ├── static/
│ │ ├── main.css # Custom CSS
│ │ ├── yourblog.png # Logo/image
│ │ ├── yourblogTran.png# Logo/image
│ │ └── profile_pics/ # Uploaded profile pictures
│ └── templates/
│ ├── layout.html # Base template
│ ├── home.html # Home page (all posts)
│ ├── about.html # About page
│ ├── register.html # Registration form
│ ├── login.html # Login form
│ ├── account.html # User account/profile
│ ├── create_post.html# Create/update post
│ └── post.html # Single post view
│
├── instance/
│ └── site.db # SQLite database file
│
├── migrations/ # Database migration scripts
│
└── run.py # App entry point
-
Clone the repository:
git clone <repo-url> cd Your_Blog
-
Create a virtual environment and activate it:
python3 -m venv venv source venv/bin/activate -
Install dependencies: (Create a
requirements.txtwith the following if not present)Flask Flask-SQLAlchemy Flask-Migrate Flask-Bcrypt Flask-Login Flask-WTF Pillowpip install -r requirements.txt
-
Set up the database:
flask db init flask db migrate -m "Initial migration." flask db upgrade -
Run the application:
python run.py
The app will be available at
http://127.0.0.1:5000/.
id: Primary keyusername: Unique usernameemail: Unique emailimage_file: Profile picture filename (default:default.jpg)password: Hashed passwordposts: Relationship toPost(one-to-many)
id: Primary keytitle: Post titledate_posted: Timestamp (default: now)content: Post contentuser_id: Foreign key toUser
All forms use Flask-WTF and WTForms for validation and CSRF protection.
- RegistrationForm: Username, email, password, confirm password
- LoginForm: Email, password, remember me
- UpdateAccountForm: Username, email, profile picture
- PostForm: Title, content
/or/home: Home page, shows all posts/about: About page/register: User registration/login: User login/logout: Log out/account: View/update user profile (login required)/post/new: Create a new post (login required)/post/<post_id>: View a single post/post/<post_id>/update: Update a post (author only)/post/<post_id>/delete: Delete a post (author only)
Key Features:
- Authentication: Only logged-in users can create, update, or delete posts.
- Authorization: Only the author can edit or delete their posts.
- Profile Picture Upload: Users can upload a new profile picture, which is resized and saved securely.
- Flash Messages: User actions (register, login, post, update) provide feedback via flash messages.
- layout.html: Base template, includes navigation and flash messages.
- home.html: Lists all posts.
- about.html: Static about page.
- register.html: Registration form.
- login.html: Login form.
- account.html: User profile and update form.
- create_post.html: Form for creating or updating posts.
- post.html: Displays a single post.
Templates use Jinja2 for dynamic content and template inheritance.
- main.css: Custom styles for the app.
- profile_pics/: Uploaded user profile images.
- yourblog.png, yourblogTran.png: App logos/images.
Database migrations are managed with Flask-Migrate (Alembic). Migration scripts are in the migrations/ directory.
To create or upgrade the database, use:
flask db migrate -m "Message"
flask db upgrade-
App Initialization:
flaskblog/__init__.pysets up the Flask app, configures the database, initializes extensions (SQLAlchemy, Migrate, Bcrypt, LoginManager), and imports routes. -
User Authentication:
Users register and log in. Passwords are hashed with Bcrypt. Flask-Login manages sessions and restricts access to certain routes. -
CRUD Operations:
Authenticated users can create, update, and delete their own posts. All posts are visible on the home page. -
Profile Management:
Users can update their username, email, and profile picture. Uploaded images are resized and stored securely. -
Forms & Validation:
All user input is validated using WTForms, with custom validation for unique usernames/emails. -
Templates & Static Files:
Jinja2 templates render dynamic content. Static files (CSS, images) are served from thestatic/directory.
- Add pagination for posts
- Add comments or tags to posts
- Implement search functionality
- Add email confirmation for registration
- Use environment variables for configuration (e.g., secret key, database URI)
- Add automated tests (unit/integration)
- Deploy to a cloud platform (Heroku, AWS, etc.)
This project is for educational purposes.
Feel free to use, modify, and share!