34 lines
1.2 KiB
Python
34 lines
1.2 KiB
Python
from flask import Blueprint, render_template, redirect, url_for, flash, request
|
|
from flask_login import login_required, current_user
|
|
from app import db
|
|
from app.models import Article
|
|
from app.forms import ArticleForm
|
|
|
|
articles_bp = Blueprint('articles', __name__)
|
|
|
|
# 文章发布
|
|
@articles_bp.route('/create', methods=['GET', 'POST'])
|
|
@login_required
|
|
def create_article():
|
|
form = ArticleForm()
|
|
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()
|
|
flash('文章发布成功', 'success')
|
|
return redirect(url_for('index'))
|
|
return render_template('articles/create.html', form=form)
|
|
|
|
# 查看文章列表
|
|
@articles_bp.route('/')
|
|
def articles():
|
|
page = request.args.get('page', 1, type=int)
|
|
articles = Article.query.paginate(page, 10, False)
|
|
return render_template('articles/index.html', articles=articles.items)
|
|
|
|
# 查看文章详情
|
|
@articles_bp.route('/<int:article_id>')
|
|
def article_detail(article_id):
|
|
article = Article.query.get_or_404(article_id)
|
|
return render_template('articles/detail.html', article=article)
|