Flask入门小项目 - 搭建极简博客(6)添加删除文章功能

目录:

完整程序点这

零、效果

登录后即可进入Delete页面,输入文章id删除成功后,再到主页看那篇文章就不见了

一、修改app.py中的delete函数

@app.route('/delete', methods=['GET', 'POST'])
@login_required
def delete():
    error = None
    if request.method == 'POST':
        article_id = request.form.get('article_id')
        if len(article_id) == 0:
            error = 'please input article id'
        else:
            article = Article.query.filter(Article.id == article_id).all()
            if len(article) == 0:
                error = 'this article id does not exist'
            else:
                db.session.delete(article[0])
                db.session.commit()
                flash('this article has been successfully deleted')
                return redirect(url_for('delete'))

    return render_template('delete.html', error=error)

二、delete.html

delete.html

{% extends "base.html" %}

{% block body %}
    <h2>Delete</h2>
    {% if error %}
          <p class=error><strong>Error:</strong> {{ error }}
    {% endif %}

    <form action="{{ url_for('delete') }}" method=post>
      <dl>
        <dt>input article id:
        <dd><input type=text size=30 name=article_id>
        <dd><input type=submit value=Delete>
      </dl>
    </form>

{% endblock %}

Final

至此,这个极简博客就完成了
接下来把它部署到阿里云上,并实现外网访问
先到pycharm底下的Terminal中输入
pip freeze >requirements.txt
生成本项目的依赖文件,方便后面移植

posted on 2020-04-11 16:33  HolaWorld  阅读(413)  评论(0编辑  收藏  举报

导航