用flask写一个图文直播网页(五)

2018-05-03  本文已影响0人  reknow

实现图文管理功能

首先贴一下图文管理界面前端代码:
list-admin.html

{% extends 'base.html' %}
{% block yemian %}图文管理界面{% endblock %}
{% block main %}
<div class="row">
    <form action="" enctype="multipart/form-data" method="POST" class="form-horizontal">
        <table class="table table-hover">
            <tr>
                <th>序号</th>
                <th>图片</th>
                <th>文章</th>
                <th>提交时间</th>
                <th>操作</th>
            </tr>
                {% for item in photo %}
                <tr>
                    <td>{{ loop.index }}</td>
                    <td width="15%">
                        <img src="{{ url_for('static', filename = 'image/' + item.picture_path) }}" alt="" height="70" width="100">
                    </td>
                    <td width="50%" height="87">{{ item.content }}</td>
                    <td>{{ item.create_time }}</td>
                    <td>
                        <a href="{{ url_for('detail_a', photo_id = item.id) }}" class="btn btn-default btn-xs" title="查看"><i class="fa fa-search"></i></a>
                        <a href="{{ url_for('change_a', photo_id = item.id) }}" class="btn btn-default btn-xs" title="修改"><i class="fa fa-pencil"></i></a>
                        <a href="{{ url_for('remove', content=item.content) }}" class="btn btn-default btn-xs" title="删除"><i class="fa fa-trash"></i></a>
                    </td>
                </tr>
                {% endfor %}
        </table>
    </form>
</div>
{% endblock %}

功能函数部分

# 图文管理
@app.route('/admin/list/', methods = ['GET', 'POST'])
def list_a():
admin2_id = session.get('user_id')
if admin2_id:
    if request.method == 'GET':
        data_all = Article.query.order_by('-create_time').all()
        items = {
            'photo': data_all
        }
        return render_template('list-admin.html', **items)
else:
    return redirect(url_for('login_a'))

# 删除图文
@app.route('/remove/')
def remove():
content = request.args.get('content')
photo1 = Article.query.filter(Article.content == content).first()
db.session.delete(photo1)
db.session.commit()
return redirect(url_for('list_a'))

# 查看图文
@app.route('/detail/<photo_id>/')
def detail_a(photo_id):
photo_model = Article.query.filter(Article.id == photo_id).first()
return render_template('detail.html', photo = photo_model)

# 修改图文
def allowed_file1(filename1):
return '.' in filename1 and filename1.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS

@app.route('/change/', methods = ['GET', 'POST'])
def change_a():
admin3_id = session.get('user_id')
if admin3_id:
    if request.method == 'GET':
        return render_template('change.html')
    else:
        file1 = request.files['file']
        content1 = request.form.get('content')
        photo_id = request.args.get('photo_id')
        if file1:
            if allowed_file(file1.filename):
                filename = secure_filename(file1.filename)
                file1.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            else:
                return u'请按要求重新选择要上传的文件!'
        else:
            pass
        photo1 = Article.query.filter(Article.id == photo_id).first()
        if file1:
            photo1.picture_path = file1.filename
        if content1:
            photo1.content = content1
        db.session.commit()
        return redirect(url_for('list_a'))
else:
    return redirect(url_for('login_a'))

一开始本来打算做个图文一起上传的,结果想想觉得图文直播大多数都是图片居多,而且很多时候下面并没有解释的文字,所以我觉得还是图片跟文字分开,各改各的。这里图片在后台显示是根据数据库中存储的图片名字,然后再去image文件中获取对应的图片。

上一篇下一篇

猜你喜欢

热点阅读