python简单博客系统源码

爱谁谁
发布: 2024-09-07 12:29:00
原创
550人浏览过
该篇文章介绍了一个使用Python语言编写的简单博客系统源码,该系统采用MVC架构,使用SQLAlchemy进行数据库操作。数据库包含3个表:用户表、文章表和评论表。应用逻辑主要包含在models.py、routes.py和views.py文件中。用户界面包括登录页面、文章列表页面、文章详细页面、创建文章页面和评论文章页面。部署到Web服务器使用以下命令:pip install -r requirements.txt,python app.py。

python简单博客系统源码

Python简单博客系统源码

引言
对于初学者来说,构建一个博客系统可以提升技术能力。本文提供了一个用Python编写的简单博客系统源码,供学习者参考。

系统架构
该系统采用MVC架构,将应用程序逻辑、数据和用户界面分开。

数据库设计
系统使用SQLAlchemy进行数据库操作。数据库包含以下表:

立即学习Python免费学习笔记(深入)”;

  • 用户表:存储用户登录信息和角色
  • 文章表:存储文章标题、内容和作者
  • 评论表:存储对文章的评论和评论者

应用程序逻辑

  • models.py:定义数据库模型
  • routes.py:定义应用程序路由和处理请求的函数
  • views.py:提供用户界面模板

用户界面

  • 登录页面:用户登录
  • 文章列表页面:显示所有文章
  • 文章详细页面:显示特定文章和评论
  • 创建文章页面:创建新文章
  • 评论文章页面:在文章上发表评论

部署
系统可以使用以下命令部署到Web服务器:

pip install -r requirements.txt
python app.py
登录后复制

源码
以下是最小可行代码(MVC):

models.py

from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    username = Column(String(255), unique=True)
    password = Column(String(255))

class Article(Base):
    __tablename__ = 'articles'
    id = Column(Integer, primary_key=True)
    title = Column(String(255))
    content = Column(String)
    author_id = Column(Integer, ForeignKey('users.id'))
    author = relationship("User", back_populates="articles")

class Comment(Base):
    __tablename__ = 'comments'
    id = Column(Integer, primary_key=True)
    content = Column(String)
    article_id = Column(Integer, ForeignKey('articles.id'))
    author_id = Column(Integer, ForeignKey('users.id'))
    author = relationship("User", back_populates="comments")
    article = relationship("Article", back_populates="comments")
登录后复制

views.py

from flask import render_template, request, redirect, url_for, flash
from . import app
from .models import User, Article, Comment
from .forms import LoginForm, CreateArticleForm, CreateCommentForm
from flask_login import current_user, login_user, logout_user, login_required

@app.route('/')
def index():
    articles = Article.query.all()
    return render_template('index.html', articles=articles)

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user and user.check_password(form.password.data):
            login_user(user)
            return redirect(url_for('index'))
    return render_template('login.html', form=form)

@app.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('login'))

@app.route('/create-article', methods=['GET', 'POST'])
@login_required
def create_article():
    form = CreateArticleForm()
    if form.validate_on_submit():
        article = Article(title=form.title.data, content=form.content.data, author=current_user)
        db.session.add(article)
        db.session.commit()
        return redirect(url_for('index'))
    return render_template('create_article.html', form=form)

@app.route('/article/<int:id>', methods=['GET', 'POST'])
def article(id):
    article = Article.query.get_or_404(id)
    form = CreateCommentForm()
    if form.validate_on_submit():
        comment = Comment(content=form.content.data, author=current_user, article=article)
        db.session.add(comment)
        db.session.commit()
        return redirect(url_for('article', id=id))
    return render_template('article.html', article=article, form=form)
登录后复制

以上就是python简单博客系统源码的详细内容,更多请关注php中文网其它相关文章!

python速学教程(入门到精通)
python速学教程(入门到精通)

python怎么学习?python怎么入门?python在哪学?python怎么学才快?不用担心,这里为大家提供了python速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号