7.18作业
发布人:shili8
发布时间:2025-01-23 03:37
阅读次数:0
**第7 周作业**
**题目描述**
本周作业要求完成一个基于 Python 的 Web 应用程序,使用 Flask 框架开发。该应用程序将提供一个简单的博客系统,允许用户注册、登录、发布文章和评论。
**任务要求**
1. 使用 Flask 框架开发一个基本的博客系统。
2. 实现用户注册和登录功能。
3. 提供发布文章和评论功能。
4. 实现文章列表和单篇文章查看功能。
5. 使用 SQLite 数据库存储用户信息和文章数据。
**代码实现**
###1. 安装依赖首先,我们需要安装 Flask 和 Flask-SQLAlchemy 库:
bashpip install flask flask-sqlalchemy
###2. 配置 Flask 应用程序创建 `app.py` 文件,配置 Flask 应用程序:
from flask import Flask, render_template, request, redirect, url_forfrom flask_sqlalchemy import SQLAlchemyapp = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db' db = SQLAlchemy(app)
###3. 定义模型定义 User 和 Article 模型:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, nullable=False)
password = db.Column(db.String(128), nullable=False)
class Article(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(128), nullable=False)
content = db.Column(db.Text, nullable=False)
author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
###4. 实现用户注册和登录功能定义 register 和 login 视图函数:
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
username = request.form['username']
password = request.form['password']
user = User(username=username, password=password)
db.session.add(user)
db.session.commit()
return redirect(url_for('login'))
return render_template('register.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, password=password).first()
if user:
return redirect(url_for('index'))
return render_template('login.html')
###5. 提供发布文章和评论功能定义 publish_article 和 comment 视图函数:
@app.route('/publish', methods=['GET', 'POST'])
def publish_article():
if request.method == 'POST':
title = request.form['title']
content = request.form['content']
author_id = request.form['author_id']
article = Article(title=title, content=content, author_id=author_id)
db.session.add(article)
db.session.commit()
return redirect(url_for('index'))
return render_template('publish.html')
@app.route('/comment', methods=['GET', 'POST'])
def comment():
if request.method == 'POST':
article_id = request.form['article_id']
content = request.form['content']
comment = Comment(article_id=article_id, content=content)
db.session.add(comment)
db.session.commit()
return redirect(url_for('index'))
###6. 实现文章列表和单篇文章查看功能定义 index 和 article 视图函数:
@app.route('/')
def index():
articles = Article.query.all()
return render_template('index.html', articles=articles)
@app.route('/article/<int:article_id>')
def article(article_id):
article = Article.query.get_or_404(article_id)
return render_template('article.html', article=article)
###7. 运行应用程序最后,运行 Flask 应用程序:
bashpython app.py
访问 ` 查看博客系统。
**注意**
本周作业要求完成一个基本的博客系统。请根据自己的需求和兴趣继续开发和完善该系统。
**参考资料**
* Flask 框架文档: />* Flask-SQLAlchemy 库文档: />* SQLite 数据库文档:

