管理信息系统 课程设计——201502010152陈斯璐

管理信息系统课程设计

1.系统该要说明

  本次项目主要完成了一个文章网站,其主要功能包括:用户注册、登录、文章发布、首页展示、文章分类展示、用户评论、点赞、收藏、用户个人中心、用户全部文章、全部评论、收藏点赞展示、个人密码修改、头像上传、热门文章推荐、关键字查询以及根据分类、根据发布年份进行组合查询等功能。

  本次系统使用pycharm作为开发工具,继续延用上学期python+flask+mysql的web网站搭建技术,网站前端采用div+css布局,添加bootstrap框架让整体看起来更加美观。

2、网站结构设计

  网站主要分为以下四个模块:展示模块、搜索模块、文章模块和用户个人中心模块。

  展示模块:

  首页展示包括顶部导航、全部文章展示区、热门文章推荐区以及文章按分类展示区。

  

  搜索模块:

  搜索模块主要分为关键字搜索即模糊查询功能,以及根据时间、分类下拉列表去进行组合查询。

    

  

  文章模块:

  文章模块主要包括用户写贴、发帖、对文章进行评论、点赞、收藏操作,推荐文章展示以及评论展示等。

  用户需登陆成功才能进行发帖等一些列操作,如未登录只能看到首页文章展示,分类展示区以及搜索功能,但不能发帖和进入文章详情页查看。


  用户个人中心模块:

  个人中心模块一共有登录、注册、个人中心的个人信息、全部文章、全部评论、全部收藏四个部分的栏目展示以及头像密码修改等功能操作部分。

3、模块详细设计

  展示模块:

  首页主py文件:

@app.route('/')
def index():
    if request.args.get('info'):
        info = request.args.get('info')
    else:
        info = None;
    content = {
        'questions': Question.query.order_by('-creat_time').all(),
        'cf': Cf.query.all(),
        'info': info,
        'hot': Question.query.order_by('-click').all()[0:5]
    }
    return render_template('index.html', **content)
@app.route('/c/<cf>')
def c(cf):
    content = {
        'questions': Question.query.filter(Question.cf == cf).order_by('-creat_time').all(),
        'cf': Cf.query.all(),
        'hot': Question.query.order_by('-click').all()[0:5]
    }
    return render_template('index.html', **content)

  首页html:

<div class="container wp">
        <div class="row clearfix">
            <div class="col-md-2 column wp-right">
                <div class="pull-right" style="width: 300px;">
                    <div>
                        <a href="#" class="list-group-item active">
                            热门推荐
                        </a>
                        {% for q in hot %}
                            <a href="{{ url_for('detail',question_id=q.id) }}" class="list-group-item">
                                <span style="font-weight: bold;font-size: 16px;color: red;padding-right: 10px;">{{ loop.index }}</span>
                                {{ q.title }}
                            </a>
                        {% endfor %}
                    </div>
                </div>
            </div>

            <div class="col-md8 column wp-left">
                 <div class="types">
                    <img src="{{ url_for("static",filename="img/types.jpg") }}"/>
                    <ul class="types-in">
                        <li>
                            {% for c in cf %}
                                <a href="{{ url_for('c',cf=c.id) }}">{{ c.name }}</a>
                            {% endfor %}
                        </li>
                    </ul>
                </div>

                <ul class="list-group">
                    {% for foo in questions %}
                        <li class="list-group-item">
                        <a class="wrap-img" href="#" target="_blank">
                            <img src="{{ url_for("static",filename="uploads/"+foo.author.icon) }}" width="50px">
                        </a>
                        <span class="glyphicon glyphicon-left" aria-hidden="true"></span>
                        <a href="{{ url_for('comment',user_id=foo.author.id ,num='1') }}"
                           target="_blank">{{ foo.author.username }}
                        </a>
                        <span class="badge">{{ foo.creat_time }}</span>
                        <hr>
                        <h1><a href="{{ url_for('detail',question_id=foo.id) }}">{{ foo.title }}</a></h1>

                        <p style="">{{ foo.detail[0:50] }}...
                        </p>
                        <div>
                            <strong style="font-family: 宋体;font-weight: normal;font-size: 12px;">分类:{{ foo.cfClass.name }}</strong>
                            <strong style="font-family: 宋体;font-weight: normal;font-size: 12px;">浏览数:{{ foo.look }}</strong>

                            <strong style="font-family: 宋体;font-weight: normal;font-size: 12px;">点赞数:{{ foo.click }}</strong>
                        </div>
                    {% endfor %}
                </ul>
            </div>

        </div>
    </div>

  js:

<script>
        {% if info %}
            alert('{{ info }}')
        {% endif %}
</script>

  搜索模块:

  搜索主py文件:

@app.route('/search')
def search():
    qu = request.args.get('q')
    c = '' if request.args.get('c') == '' else request.args.get('c')
    y = '' if request.args.get('y') == '' else request.args.get('y')
    query = Question.query.filter(
        or_(
            Question.title.contains(qu),
            Question.detail.contains(qu),
        ),
        Question.cf.like('%' + c + '%'),
        Question.creat_time.like('%' + y + '%'),
    ).order_by('-creat_time').all()
    content = {
        'questions': query,
        'cf': Cf.query.all(),
        'hot': Question.query.order_by('-click').all()[0:5]
    }
    return render_template('index.html', **content)

  文章模块:

  文章详情页主py文件:

@app.route('/detail/<question_id>', methods=['GET', 'POST'])
@loginFirst
def detail(question_id):
    quest = Question.query.filter(Question.id == question_id).first()
    u = User.query.filter(User.id == session.get('user_id')).first()
    if request.method == 'POST':
        if request.form.get('click') == '1':
            quest.click = quest.click + 1
        if request.form.get('collection') == '1':
            user = u
            user.collection.append(quest)
            db.session.add(user)
    col = u.collection.filter_by(id=question_id).first()
    if col is None:
        col = {}
    comment = Comment.query.filter(Comment.question_id == question_id).order_by('-creat_time').all()
    quest.look = quest.look + 1
    content = {
        'ques': quest,
        'comment': comment,
        'col': col,
        'questions': Question.query.filter(Question.cf == quest.cf).all(),
    }
    return render_template('detail.html', **content)

  文章详情页html:

<div class="container">
        <div class="row clearfix"><div class="col-md-8 column wp-middle">
                <h1 style="text-align: center">{{ ques.title }}</h1>
                <div style="padding: 10px;text-align: center">
                    <span style="padding-left: 10px;">作者:{{ ques.author.username }}</span>
                    <span style="padding-left: 10px;">浏览数:{{ ques.look }}</span>
                    <span style="padding-left: 10px;">点赞数:{{ ques.click }}</span>
                    <form method="post" style="display: inline-block;padding-left: 10px;">
                        <input name="click" value="1" type="hidden">
                        <button>点赞</button>
                    </form>
                    <form method="post" style="display: inline-block;padding-left: 10px;">
                        <input name="collection" value="1" type="hidden">
                        {% if col %}
                            <button type="button" disabled>已收藏</button>
                        {% else %}
                            <button>收藏</button>
                        {% endif %}
                    </form>
                </div>
                <div style="padding: 50px 50px;margin-bottom: 100px;border:1px solid #eee;border-radius:4px">
                    <p>
                        {{ ques.detail }}

                    </p>
                </div>
                <hr>
                <div style="padding: 15px;margin-left:20px">
                    <h1 style="text-align: center;font-size: 20px">推荐文章</h1><br>
                    {% for foo in questions %}
                        <li class="list-group-item" style="width: 700px">
                            <a class="wrap-img" href="#" target="_blank">
                                <img src="{{ url_for("static",filename="uploads/"+foo.author.icon) }}" width="50px">
                            </a>
                            <span class="glyphicon glyphicon-left" aria-hidden="true"></span>
                            <a href="{{ url_for('comment',user_id=foo.author.id ,num='1') }}"
                               target="_blank">{{ foo.author.username }}</a>
                            <span class="badge">{{ foo.creat_time }}</span>
                            <br>
                            <h3>
                                <a href="{{ url_for('detail',question_id=foo.id) }}">{{ foo.title }}</a>
                            </h3>


                            <p style="">{{ foo.detail[0:50] }}...
                            </p>
                        </li>
                    {% endfor %}
                </div>

                <hr>
                <form class="form-horizontal" role="form" method="post" action="{{ url_for('answer') }}">
                    <h1 style="text-align: center;font-size: 20px">评论区</h1>
                    <br>
                    <div class="spacing">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label pinglunbox" style="font-family: 宋体;font-weight: normal;">评论内容</label>
                        <div class="col-sm-10">
                            <input type="text" name="author_id" value="{{ user.id }}" hidden>
                            <input type="text" name="question_id" value="{{ ques.id }}" hidden>
                            <textarea class="form-control" name="detail" rows="10" style="width: 484px; height: 119px;"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default">发布</button>
                        </div>
                    </div>
                    </div>
                </form>
                <hr>
{#            用户评论区#}

                <h1 style="text-align: center;font-size: 20px">用户评论</h1><br>
                <ul class="list-group" style="margin-left:20px">
                    {% for com in comment %}
                        <li class="list-group-item" style="width: 700px;">
                             <a class="wrap-img" href="#" target="_blank">
                                <img src="{{ url_for("static",filename="uploads/"+com.author.icon) }}" width="40px" height="40px">
                            </a>
                            <span class="glyphicon glyphicon-left" aria-hidden="true"></span>
                            <a href="{{ url_for('comment',user_id=com.author.id,num='1') }}"
                               target="_blank">{{ com.author.username }}</a>
                            <span class="badge">{{ com.creat_time }}</span>
                            <br>
                            <p style="">{{ com.detail }}</p>
                        </li>
                    {% endfor %}
                </ul>
            </div></div>
    </div>

  发帖主py文件:

@app.route('/question', methods=['GET', 'POST'])
@loginFirst
def question():
    if request.method == 'GET':
        cf = Cf.query.all()
        return render_template('question.html', cf=cf)
    else:
        title = request.form.get('title')
        detail = request.form.get('detail')
        author_id = request.form.get('author_id')
        cf = request.form.get('cf')
        question = Question(title=title, detail=detail, author_id=author_id, cf=cf)
        db.session.add(question)  # 加入数据库
        db.session.commit()
        return redirect(url_for('index'))

  发帖html:

<div class="container">
        <div class="row clearfix">

            <div class="col-md-8 column wp-middle">
                <form class="form-horizontal" role="form" method="post" action="#">
                <div class="spacing">
                    <div class="form-group">
                        <h2 style="text-align: center">发帖</h2>
                        <br>
                        <label for="inputEmail3" class="col-sm-2 control-label">标题</label>
                        <div class="col-sm-10">
                            <input type="text" name="author_id" value="{{ user.id }}" hidden>
                            <input type="text" class="form-control" id="title" name="title"/>
                        </div>
                    </div>


                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">内容</label>
                        <div class="col-sm-10">
                            <textarea class="form-control" name="detail" rows="10"></textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <select name="cf" id="" class="form-control">
                                <option value="">--请选择分类--</option>
                                {% for c in cf %}
                                    <option value="{{ c.id }}">{{ c.name }}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default">发布</button>
                        </div>
                    </div>
                 </div>
                </form>
            </div>

        </div>
    </div>

  评论主py文件:

@app.route('/answer/', methods=['GET', 'POST'])
def answer():
    if request.method == 'POST':
        question_id = request.form.get('question_id')
        author_id = request.form.get('author_id')
        detail = request.form.get('detail')
        comment = Comment(question_id=question_id, author_id=author_id, detail=detail)
        db.session.add(comment)
        db.session.commit()
        return redirect(url_for('detail', question_id=question_id)) 

  

  用户个人中心模块:

  登录主py文件:

@app.route('/login/', methods=['GET', 'POST'])
def login():
    if request.method == 'GET':
        return render_template('login.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        user = User.query.filter(User.username == username).first()
        if user:
            if user.check_password(password):
                session['username'] = user.username
                session['user_id'] = user.id
                session.permanent = True
                # 重新定位到首页
                return redirect(url_for('index'))
            else:
                return '用户密码错误'# 重新定位到注册
                return redirect(url_for('login'))
        else:
            return redirect(url_for('regist'))

  登录html:

<div class="container">
        <div class="row clearfix">

            <div class="col-md-8 column wp-middle">
                <form class="form-horizontal" role="form" method="post" action="{{ url_for('login') }}">
                    <h1 style="text-align: center;font-size: 20px">登录</h1>
                    <br>
                    <div class="spacing">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">账号</label>
                        <div class="col-sm-10">
                            <input type="text" name="username" class="form-control" id="inputEmail3"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-10">
                            <input type="password" name="password" class="form-control" id="inputPassword3"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <div class="checkbox">
                                <label><input type="checkbox"/>Remember me</label>
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                              <button type="submit" class="btn btn-default">登录</button>
                        </div>
                    </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

  注册主py文件:

@app.route('/regist/', methods=['GET', 'POST'])
def regist():
    if request.method == 'GET':
        # 打开注册页的模板
        return render_template('regist.html')
    else:  # 收到用户上传的信息
        username = request.form.get('username')
        password = request.form.get('password')
        user = User.query.filter(User.username == username).first()
        if user:
            return '该用户已存在'
        else:
            user = User(username=username, password=password)
            db.session.add(user)  # 加入数据库
            db.session.commit()
            return redirect(url_for('login'))

  注册html:

<div class="container">
        <div class="row clearfix">
            <div class="col-md-8 column wp-middle">
                <form class="form-horizontal" role="form" method="post" action="{{ url_for('regist') }}">
                   <h1 style="text-align: center;font-size: 20px">注册</h1>
                    <br>
                    <div class="spacing">
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">账号</label>
                        <div class="col-sm-10">
                            <input type="text" name="username" class="form-control" id="inputEmail3" required/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="Password1" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-10">
                            <input type="password" name="password" class="form-control" id="Password1" required/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="Password2" class="col-sm-2 control-label">确认密码</label>
                        <div class="col-sm-10">
                            <input type="password"  class="form-control" id="Password2" required/>
                        </div>
                    </div>
                    <div id="error_box1">
                        <br>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" onclick="aaa()" class="btn  btn-default ">注册</button></div>
                    </div>
                </div>
        </form>
    </div>
    </div>
    </div>

  个人中心py文件:

@app.route('/comment/<user_id>/<num>')
def comment(user_id, num):
    user = User.query.filter(User.id == user_id).first()
    content = {
        'comment': user.comment,
        'questions': user.question,
        'user2': user,
    }
    if (num == '1'):
        return render_template('subComment1.html', **content, title='全部文章')
    elif (num == '2'):
        return render_template('subComment2.html', **content)
    elif (num == '3'):
        return render_template('subComment3.html', **content)
    elif (num == '4'):
        content = {
            'comment': user.comment,
            'questions': user.collection.all(),
            'user2': user,
        }
        return render_template('subComment1.html', **content, title='我的收藏')
    else:
        return render_template('subComment1.html', **content)

  个人中心html:

<div class="container">
        <div class="row clearfix">

            <div class="col-md-8 column wp-middle">
                <ul class="types-in">
                    <li role="presentation"><a href="{{ url_for('comment',user_id=user2.id,num='1') }}">全部文章</a></li>
                    <li role="presentation"><a href="{{ url_for('comment',user_id=user2.id,num='2') }}">全部评论</a></li>
                    <li role="presentation"><a href="{{ url_for('comment',user_id=user2.id,num='3') }}">个人信息</a></li>
                    <li role="presentation"><a href="{{ url_for('comment',user_id=user2.id,num='4') }}">我的收藏</a></li>
                </ul>
                {% block subComment %}{% endblock %}
            </div>

        </div>
    </div>

  个人中心-个人信息html:

<div class="container">
        <div class="row clearfix">

            <div class="col-md-8 column wp-middle">
                <ul class="types-in">
                    <li role="presentation"><a href="{{ url_for('comment',user_id=user2.id,num='1') }}">全部文章</a></li>
                    <li role="presentation"><a href="{{ url_for('comment',user_id=user2.id,num='2') }}">全部评论</a></li>
                    <li role="presentation"><a href="{{ url_for('comment',user_id=user2.id,num='3') }}">个人信息</a></li>
                    <li role="presentation"><a href="{{ url_for('comment',user_id=user2.id,num='4') }}">我的收藏</a></li>
                </ul>
                {% block subComment %}{% endblock %}
            </div>

        </div>
    </div>

  个人中心-全部帖子和我的收藏html:

<ul class="list-group" style="margin-top: 60px;margin-left:20px">
        <h2>{{ title }}</h2>
        {% for foo in questions %}
            <li class="list-group-item">
                <a class="wrap-img" href="#" target="_blank">
                    <img src="{{ url_for("static",filename="uploads/"+foo.author.icon) }}" width="40px" height="40px">
                </a>
                <span class="glyphicon glyphicon-left" aria-hidden="true"></span>
                <a href="{{ url_for('comment',user_id=foo.author.id ,num='1')}}"
                   target="_blank">{{ foo.author.username }}</a>
                <span class="badge">{{ foo.creat_time }}</span>
                <br>
                <h3>
                    <a href="{{ url_for('detail',question_id=foo.id) }}">{{ foo.title }}</a>
                </h3>


                <p style="">{{ foo.detail[0:50] }}...
                </p>
            </li>
        {% endfor %}
    </ul>

  个人中心-全部评论html:

<ul class="list-group" style="margin-top: 60px;margin-left:20px">
        <h2>全部评论</h2>
        {% for com in comment %}
            <li class="list-group-item">
                <a class="wrap-img" href="#" target="_blank">
                    <img src="{{ url_for("static",filename="uploads/"+com.author.icon) }}" width="40px" height="40px">
                </a>
                <span class="glyphicon glyphicon-left" aria-hidden="true"></span>
                <a href="{{ url_for('comment',user_id=com.author.id,num='1') }}"
                   target="_blank">{{ com.author.username }}</a>
                <span class="badge">{{ com.creat_time }}</span>
                <br>

                <p style="">{{ com.detail }}</p>
            </li>
        {% endfor %}
    </ul>

  个人中心-个人信息html:

div class="list-group" style="margin-top: 60px;margin-left:20px">
        <h2>名称:
            <small style="font-size: 18px">{{ user2.username }}</small>
        </h2>
        <h2>问题数:
            <small style="font-size: 18px">{{ questions|length }}</small>
        </h2>
        <h2>评论数:
            <small style="font-size: 18px">{{ comment|length }}</small>
        </h2>
    </div>

  修改密码和头像上传html:

<div class="container">
        <div class="row clearfix">

            <div class="col-md-8 column wp-middle">
                <div style="margin: 80px 0px;margin-left: 70px;">
                    <h1>上传头像</h1>
                    <form class="form-horizontal" role="form" method="post" enctype="multipart/form-data" action="{{ url_for('uploadLogo',user_id=user_id) }}">

                        <div class="form-group">
                            <div class="col-sm-10">
                                <input type="file" id="exampleInputFile" name="logo" required>
                                <button type="submit" class="btn btn-default">uploadLogo</button>
                                <img src="{{ url_for("static",filename="uploads/"+user.icon ) }}" width="40px" height="40px"/>
                            </div>
                        </div>

                    </form>
                </div>
                <div style="margin: 80px 0px;margin-left: 70px;">
                <form class="form-horizontal" role="form" method="post">
                    <h1>修改密码</h1>
                    <div class="form-group">
                        <label for="inputEmail3" class="col-sm-2 control-label">原密码</label>
                        <div class="col-sm-10">
                            <input type="password" name="old" class="form-control" id="p1" required/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-2 control-label">新密码</label>
                        <div class="col-sm-10">
                            <input type="password" name="new1" class="form-control" id="p2" required/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword3" class="col-sm-2 control-label">确定密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="p3" required/>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">
                            <button type="submit" class="btn btn-default" onclick="return f()">submit</button>
                        </div>
                    </div>
                </form>
                </div>
            </div>

        </div>
    </div>
<script>
        function f() {

            if ($('#p2').val() == $('#p3').val())
                return true;
            alert('两次密码不一样');
            return false;
        }
        window.alert("修改成功!");

 </script>

  修改密码主py文件:

@app.route('/setPassword/<id>', methods=['GET', 'POST'])
@loginFirst
def setPassword(id):
    if request.method == 'GET':
        return render_template('setPassword.html')
    else:
        user = User.query.filter(User.id == id).first()
        if user:
            if user.check_password(request.form.get('old')):
                user.password = request.form.get('new1')
                db.session.commit()
                info = '修改成功'
            else:
                info = '原密码错误'
        else:
            info = '未知错误'
        return redirect(url_for('index', info=info))

  上传头像主py文件:

@app.route('/uploadLogo/<user_id>', methods=['GET', 'POST'])
def uploadLogo(user_id):
    user = User.query.filter(User.id == user_id).first()
    f = request.files['logo']
    basepath = os.path.dirname(__file__)  # 当前文件所在路径
    upload_path = os.path.join(basepath, 'static/uploads', f.filename)  # 注意:没有的文件夹一定要先创建,不然会提示没有该路径
    f.save(upload_path)
    user.icon = 'uploads/' + f.filename
    db.session.commit()
    return redirect(url_for('setPassword', id=user_id)) 

4、数据库设计

  本系统数据库一共建立5个表,分别是:user用户表,question帖子表,comment评论表,collection收藏表,cf分类表。

5、系统实现的关键算法和数据结构

  密码加密:

class User(db.Model):
    __tablename__ = 'user'
    # 建立一个表user
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(30), nullable=False)
    _password = db.Column(db.String(200), nullable=False)
    say = db.Column(db.String(50))
    icon = db.Column(db.String(100))
    collection = db.relationship('Question', secondary=Collection, backref=db.backref('user', lazy='dynamic'),
                                 lazy='dynamic')

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, row_password):
        self._password = generate_password_hash(row_password)

    def check_password(self, row_password):
        return check_password_hash(self._password, row_password)

  上下文处理器:

@app.context_processor
def myContext():
    username = session.get('username')
    if username:
        user = User.query.filter(User.username == username).first()
    else:
        user = {}
    if user:
        return {'user_id': user.id, 'user': user}
    else:
        return {}

6、成品展示

  界面、功能展示:

  

总结:通过这次大作业我学到了许多新的知识,对Python的Flask框架也有了更深入的了解,遇到问题还是会慌张但是在冷静下来之后也能慢慢的发现问题所在去解决了,我发现其实这个框架并不是那么的难,只要你静下心来学还是可以掌握大部分内容的。谢谢度娘,也谢谢帮助过我的老师和同学。ending~~

猜你喜欢

转载自www.cnblogs.com/silu666-666/p/9189642.html