database setup: python
Step 1: Set Up Your Environment
- Install Python: Ensure Python is installed on your system. You can download it from python.org.
- Install Flask: Open your terminal or command prompt and run:
pip install flask
Step 2: Create the Flask Project
- Create a new directory for your project:
mkdir flask_blog cd flask_blog
- Create a file named
app.py
:touch app.py
Step 3: Set Up the Flask Application
In app.py
, set up a basic Flask app:
from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Welcome to My Flask Blog!" if __name__ == '__main__': app.run(debug=True)
Run the app:
python app.py
Visit http://127.0.0.1:5000/
in your browser. You should see “Welcome to My Flask Blog!”
Step 4: Add a Database
We’ll use SQLite for simplicity and SQLAlchemy to interact with the database.
- Install the required packages:
pip install flask-sqlalchemy
- Update
app.py
to include the database setup:from flask import Flask, render_template, request, redirect, url_for from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db' db = SQLAlchemy(app) class Post(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) author = db.Column(db.String(50), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.author}')" # Create the database with app.app_context(): db.create_all() @app.route('/') def home(): posts = Post.query.all() return render_template('home.html', posts=posts) if __name__ == '__main__': app.run(debug=True)
Step 5: Create Templates
- Create a folder named
templates
in your project directory:mkdir templates
- Inside the
templates
folder, create a file namedhome.html
:<!DOCTYPE html> <html> <head> <title>Flask Blog</title> </head> <body> <h1>Welcome to My Flask Blog</h1> {% for post in posts %} <div> <h2>{{ post.title }}</h2> <p>{{ post.content }}</p> <p>By {{ post.author }}</p> </div> {% endfor %} </body> </html>
Step 6: Add a Form to Create Posts
- Update
app.py
to handle form submissions:@app.route('/create', methods=['GET', 'POST']) def create(): if request.method == 'POST': title = request.form['title'] content = request.form['content'] author = request.form['author'] new_post = Post(title=title, content=content, author=author) db.session.add(new_post) db.session.commit() return redirect(url_for('home')) return render_template('create.html')
- Create a
create.html
template in thetemplates
folder:<!DOCTYPE html> <html> <head> <title>Create Post</title> </head> <body> <h1>Create a New Post</h1> <form method="POST"> <label for="title">Title:</label><br> <input type="text" id="title" name="title"><br><br> <label for="content">Content:</label><br> <textarea id="content" name="content"></textarea><br><br> <label for="author">Author:</label><br> <input type="text" id="author" name="author"><br><br> <input type="submit" value="Submit"> </form> </body> </html>
- Add a link to the create page in
home.html
:<a href="{{ url_for('create') }}">Create New Post</a>
Step 7: Run the Application
- Run the app:
python app.py
- Visit
http://127.0.0.1:5000/
to see your blog. - Click “Create New Post” to add a new blog post.
Optional Enhancements
- Add user authentication using Flask-Login.
- Style your blog using Bootstrap or custom CSS.
- Add functionality to edit or delete posts.
- Use a more robust database like PostgreSQL or MySQL.