- 期末作品检查:必须完
- 网站父模板统一布局:头部导航条、底部图片导航、中间主显示区域布局
<!DOCTYPE html> <html > <head lang="en"> <meta charset="UTF-8"> <title>{% block title %}同袍{% endblock %}</title> <link href="{{ url_for('static',filename='CSS/base.css') }}"rel="stylesheet"type="text/css"> <script src="{{ url_for("static",filename="JS/base.js") }}"></script> {% block head %}{% endblock %} </head> <body id="myBody" > <nav class="header" id="headers"> <div> <a href="#",title="#"> <img src="{{ url_for("static",filename="images/logo.png") }}"width="120px"height="40px"> </a> </div> <div> <a href="{{ url_for("base") }}">首页</a> <a href="{{ url_for("questions") }}">发布</a> <a href="#">文章</a> <a href="https://www.hanfugou.com/"target="_blank">铺子</a> <a href="https://www.hanfugou.com/fleas/"target="_blank">二手</a> <a href="http://www.hanfuhui.cn/album/"target="_blank">摄影</a> </div> <img id="on_off" onclick="mySwitch()" src="{{ url_for("static",filename="images/bulbon.png") }}" width="40px"> <div style="float: right"> {% if username %} <a href="{{ url_for('usercenter1',user_id=session.get('userid'),tag=1) }}">{{ username }}</a> <a href="{{ url_for("logout") }}">注销</a> {% else %} <a href="{{ url_for("login") }}">登进</a>※ <a href="{{ url_for("register") }}">注册</a> {% endif %} </div> <div style="float: right"> <form action="{{ url_for('search') }}" method="get"> <input name="q" name="search" type="text" style="300px" > <button type="submit" style="height: 20px" >搜索</button> </form> </div> </nav> {% block main %} <div class="bodys" id="def_main"> <ul> {% for foo in questions %} <br> <li> <div class="content"> <div class="author"> <a href="{{ url_for("usercenter1",user_id=foo.author_id,tag=1) }}">用户名:{{ foo.author.username }}</a> <span>时间:{{ foo.creat_time }}</span><br> <a class="title" href="{{ url_for("detail",question_id=foo.id) }}">题目:{{ foo.title }}</a><br> <p>内容:{{ foo.detail }}</p><br> </div> </div> </li> {% endfor %} <br> <a href="http://www.hanfuhui.cn/album/72953"target="_blank"> <img src="{{ url_for("static",filename="images/li1.jpg") }}"width="24%" > </a> <a href="http://www.hanfuhui.cn/album/72941"target="_blank"> <img src="{{ url_for("static",filename="images/li2.jpg")}}"width="24%"> </a> <a href="http://www.hanfuhui.cn/album/73037"target="_blank"> <img src="{{ url_for("static",filename="images/li3.jpg") }}"width="24%"> </a> <a href="http://www.hanfuhui.cn/album/58620"target="_blank"> <img src="{{ url_for("static",filename="images/li4.jpg") }}"width="24%"> </a> <br> </ul> </div> {% endblock %} <footer class="copyright"> <p>版权归属@</p> </footer> </body> </html>
- 注册
@app.route('/register/', methods=['GET', 'POST'])#注册 def register(): # return render_template('register.html') if request.method == 'GET': return render_template('register.html') else: username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: return '用户名通过' else: user1 = User(username=username, password=password) db.session.add(user1) db.session.commit() return redirect(url_for('login'))
{% extends 'base.html' %} {% block title %}注册{% endblock %} {% block head %} <link href="{{ url_for('static',filename='CSS/register.css') }}" rel="stylesheet" type="text/css"> <script src="{{ url_for('static',filename='JS/register.js') }}"></script> {% endblock %} {% block main %} <div class="register"> <div class="register_inp"> <h1>注册</h1> <form action="{{ url_for('register') }}" method="post"> <input id="username" name="username" type="text" value="用户名" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = '用户名';}"> <input id="password" name="password" type="password" value="密码" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = '密码';}"> <input id="dpassword" type="password" value="确认密码" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = '确认密码';}"> <br> <div class="register_button"> <button type="submit" onclick="return MyRegister()">注册</button> <a href="{{ url_for("login") }}">登录</a> </div> </form> </div> </div> {% endblock %}
@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.password == password: session['user'] = username session['userid'] = user.id session.permanent = True return redirect(url_for('base')) else: return '密码错误' else: return '用户名不存在'
{% extends 'base.html' %} {% block title %}登录{% endblock %} {% block head %} <link href="{{ url_for('static',filename='CSS/login.css') }}" rel="stylesheet" type="text/css"> <script src="{{ url_for('static',filename='JS/login.js') }}"></script> {% endblock %} {% block main %} <div class="login"> <div class="login_inp"> <h1>登录</h1> <form action="{{ url_for('login') }}"method="post"> <input id="uname" name="username" type="text" value="用户名" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = '用户名';}"> <input id="upass" name="password" type="password" value="密码" onfocus="this.value = '';" onblur="if (this.value==''){ this.value = '密码';}"> <div class="forgot"> <input type="checkbox" checked="checked">记住我 <input type="checkbox" checked="checked">自动登录 <a href="#">·忘记密码·</a> </div> <br> <div class="login_button"> <button type="submit" onclick="return MyLogin()">登录</button> <a href="{{ url_for("register") }}">注册</a> </div> </form> </div> </div> {% endblock %}
@app.route('/logout/') def logout(): session.clear() return redirect(url_for('base'))
- 发布
@app.route('/questions/',methods=['GET','POST'])#发布 @loginFirst def questions(): if request.method == 'GET': return render_template('questions.html') else: title = request.form.get('title') detail = request.form.get('text') user=User.query.filter(User.username == session.get('user')).first() author_id = user.id question = Question.query.filter(Question.title == title).first() if question: return 'Question existed' else: question1 = Question(title=title, detail=detail, author_id=author_id) question1.author = user db.session.add(question1) # 保存到数据库 db.session.commit() # 提交 return redirect(url_for('base'))
{% extends 'base.html' %} {% block title %}讨论{% endblock %} {% block head %} <link href="{{ url_for('static',filename='CSS/questions.css') }}" rel="stylesheet" type="text/css"> {% endblock %} {% block main %} <div class="question"> <div class="question_inp"> <h1>(◕ᴗ◕✿请开始你的表演...)</h1> <form action="{{ url_for('questions') }}"method="post"> <div> <label for="ask"> (。◕ᴗ◕。标题)</label> <br> <textarea name="title" style="height: 22px; 100%" rows="1" id="ask"></textarea> </div> <div> <label for="questionDetail"> (。◕ˇ∀ˇ◕内容)</label> <br> <textarea name="text" rows="2" id="questionDetail"style=" 100%;height: 100px" ></textarea> </div> <br> <div class="question_button"> <button type="submit">发布</button> <br> </div> <br> </form> </div> </div> {% endblock %}
@app.route('/')#主页 def base(): context = { 'questions': Question.query.all() } return render_template('base.html', **context)
- 详情页
{% extends 'base.html' %} {% block title %}文章{% endblock %} {% block head %} <link href="{{ url_for('static',filename='CSS/word.css') }}" rel="stylesheet" type="text/css"> {% endblock %} {% block main %} <div class="word" id="def_main"> <form action="{{ url_for('comment') }}"method="post"> <div class="word_inp"> <h2>题目:{{ question.title }}</h2> <a href="{{ url_for('usercenter1',user_id=question.author_id,tag=1) }}">用户名:{{ question.author.username }}</a> <span>时间:{{ question.creat_time }}</span><br> <div class="p"> <p>{{ question.detail }}</p> </div> <textarea name="detail" rows="2"style=" 80%;border-radius: 5px;"></textarea> <button >发送</button> <input name="question_id" type="hidden" value="{{ question.id }}"> <ul class="ul1"> <p class="comment_num">^{{ question.comments|length }}^</p> {% for foo in question.comments %} <li> <div class="author"> <a href="{{ url_for('usercenter1',user_id=foo.author_id,tag=1) }}">用户名:{{ foo.author.username }}</a> <span>时间:{{ foo.creat_time }}</span><br> <p>评论:{{ foo.detail }}</p> </div> </li> {% endfor %} </ul> </div> </form> </div> {% endblock %}
- 评论
@app.route('/comment/',methods=['POST']) @loginFirst def comment(): detail=request.form.get('detail') author_id = User.query.filter(User.username == session.get('user')).first().id print(author_id) question_id = request.form.get('question_id') comment = Comment(author_id=author_id,question_id=question_id,detail=detail) db.session.add(comment) # 保存到数据库 db.session.commit() # 提交 return redirect(url_for('detail',question_id=question_id))
<ul class="ul1"> <p class="comment_num">^{{ question.comments|length }}^</p> {% for foo in question.comments %} <li> <div class="author"> <a href="{{ url_for('usercenter1',user_id=foo.author_id,tag=1) }}">用户名:{{ foo.author.username }}</a> <span>时间:{{ foo.creat_time }}</span><br> <p>评论:{{ foo.detail }}</p> </div> </li> {% endfor %} </ul>
- 个人中心
{% extends 'base.html' %} {% block title %}个人中心{% endblock %} {% block head %} <link href="{{ url_for('static',filename='CSS/userbase.css') }}" rel="stylesheet" type="text/css"> {% endblock %} {% block main %} <ul class="nav_ul"> <h2>{{ user.username }}</h2> <li role="presentation"><a href="{{ url_for('usercenter1',user_id=user.id,tag='1') }}">全部问答</a> </li> <li role="presentation"><a href="{{ url_for('usercenter1',user_id=user.id,tag='2') }}">全部评论</a> </li> <li role="presentation"><a href="{{ url_for('usercenter1',user_id=user.id,tag='3') }}">个人信息</a> </li> {% block user %} {% endblock %} </ul> {% endblock %}
{% extends'userbase.html' %} {% block user %} <div class="detail_left"> {% for foo in user.questions %} <span class="icon2" aria-hidden="true"></span> <a href="#" class="name">用户名:{{ foo.author.username }}</a> <span class="badge">创建时间:{{ foo.creat_time }}</span> <br> <a href="{{ url_for("detail",question_id=foo.id) }}" class="title">题目:{{ foo.title }}</a><br> <p class="wenzhang" >内容:{{ foo.detail }}</p> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div><br> {% endfor %} </div> {% endblock %}
{% extends'userbase.html' %} {% block user %} <div class="detail_left"style=""> {% for foo in user.comments %} <span class="icon2" aria-hidden="true"></span> <a href="#" class="name">用户名:{{ foo.author.username }}</a> <span class="badge"style="float: right">时间:{{ foo.creat_time }}</span> <br> <p class="wenzhang" >评论:{{ foo.detail }}</p> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> {% endfor %} </div> {% endblock %}
{% extends'userbase.html' %} {% block user %} <div class="info"> <p>用户名:{{ user.username }}</p> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> <p>编号:{{ user.id }}</p> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> <p>文章篇数:{{ user.questions|length }}</p> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> <p>评论次数:{{ user.comments|length }}</p> <div style="border-top:1px dashed black;height: 1px;overflow:hidden"></div> </div> {% endblock %}
- 搜索,条件组合搜索
@app.route('/search/') @loginFirst def search(): que=request.args.get('q') ques=Question.query.filter( or_( Question.title.contains(que), Question.detail.contains(que), ) ).order_by('-creat_time') return render_template('base.html',questions=ques)
<div style="float: right"> <form action="{{ url_for('search') }}" method="get"> <input name="q" name="search" type="text" style="300px" > <button type="submit" style="height: 20px" >搜索</button> </form> </div>
个人学期总结
- 网站父模板统一布局:头部导航条、底部图片导航、中间主显示区域布局
- Python是一种面向对象的解释型计算机程序设计语言,Python是纯粹的自由软件, 源代码和解释器CPython遵循 GPL(GNU General Public License)协议。Python语法简洁清晰,特色之一是强制用空白符(white space)作为语句缩进。Flask是一个面向简单需求小型应用的“微框架(microframework)”,Flask选择组件的额外工作给那些使用案例不适用标准ORM的开发者提供了更多的灵活性,同样也给使用不同工作流和模版化系统的开发者们带来了灵活性。MySQL是一个关系型数据库管理系统,MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 RDBMS (Relational Database Management System,关系数据库管理系统) 应用软件。MySQL是一种关系数据库管理系统,关系数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。MySQL所使用的 SQL 语言是用于访问数据库的最常用标准化语言。