一、個人學期總結
在這個學期,我由一開始的概念知識到后面基本熟悉的上機操作,都離不開老師對我們一步步的指導。在這個課程中我發現,跟隨老師的步伐,做出一個界面並不代表什么,而是需要課后多加熟悉,上手操作,才能算是掌握這門課程的核心。一開始上課時,老師首先是讓我們熟悉了解了什么是管理信息系統,以及我們眼中的管理信息系統是什么。我們學習了Python的基礎語法,輸入、輸出、交互、數字計算的方法、字符串基本操作、凱撒密碼、自制九九乘法表、中英文詞頻統計等,還學習了import turtle庫,並利用這個庫繪制出多種多樣的圖形,海龜圖標有趣可愛,課堂氛圍活潑許多,從而激發了我們學習Python的興趣,提升了我們對編程語言的思維能力,為構建Flask框架制作網頁的學習打下基礎。並讓我們從簡單的上手,做簡單輸入輸出交互,例如 用戶輸入兩個數字,計算並輸出兩個數字之和:(嘗試只用一行代碼實現這個功能) 輸入半徑,計算圓的面積。除此之外,我們還進行了turtle庫的基礎學習和循環、函數和條件定義,練習如何畫五角星 畫同心圓 畫太陽花 畫五個角星,這讓我初次嘗試到了這門課程的樂趣,並想要繼續探尋這其中的奧秘。在這之后,我們又相繼學習了如何如何統計中文和英文字頻、如何利用datetime處理時間和日期問題。基本入門后,我們開始學習認識URL,觀察常用網站網址,區分不同組成部分。觀察web瀏覽過程.,了解HTML基礎,練習使用標簽制作簡單的頁面。觀察常用網頁的HTML元素,在實際的應用場景中,用已學的標簽模仿制作。 用div,form制作登錄頁面, 練習使用下拉列表選擇框,無序列表,有序列表,定義列表。並且使用<script></script>的三種用法,登錄驗證,完成登錄與注冊頁面的HTML+CSS+JS。在學習的中后期,我們開始學習FLASK這一項目,設置調試模式,理解Flask項目主程序。使用裝飾器,設置路徑與函數之間的關系。使用Flask中render_template,用不同的路徑,返回首頁、登錄員、注冊頁。用視圖函數反轉得到URL,url_for(‘login’),完成導航里的鏈接。加載靜態文件,進行父模板的繼承和擴展,實現自己代碼的需求。
在學習Python+Flask+MysqL的web建設時,需要安裝數據庫(mysql)、開發工具(pycharm)和開發需要用到的第三方庫。一開始,我們學習網頁前端的制作,第一次知道五花八門的網頁原來是由許許多多的標簽對組成的,每一對都有它特定的一些功能,包含了標簽對里的眾多參數,要想實現這些標簽對的功能就必須遵循它的書寫規則,由於pycharm軟件里,在html里一旦有某個字母拼寫錯誤就會報錯,顏色也是書寫正誤的憑借,所以html報錯還是比較好處理的,但在JS代碼的編寫中不會提示你哪里出錯,所以在編寫過程中要多加細心謹慎。
在后端建設過程中,我們引入了flask庫,用於創建一個Flask對象以及頁面的跳轉,引入flask_sqlalchemy庫進行數據庫的關聯映射等,學習了對數據的增刪改查等操作,利用對數據庫的查詢、添加功能實現了頁面的登陸、注冊以及發布功能,這些功能的實現根據前端頁面的name屬性名,后台請求接收同名name的值,再對接收到的值做一系列增刪查改操作。在對這些name屬性命名時,要注意前后端對應,且同一頁面不能出現相同命名,否則會出現數據傳遞出錯,造成一系列的麻煩,所以在編程過程中要時刻注意代碼的唯一性、對應性、簡潔性和條理性,盡量用更少更優化的代碼實現功能。為網頁增加功能,為網頁增添屬於自己的風格。開始做Flask項目,加載靜態文件,父模板的繼承和擴展,連接mysql數據庫,創建用戶模型,建立mysql和app的連接。通過用戶模型,對數據庫進行增刪改查操作。完成注冊功能,將界面的數據存到數據庫,redirect重定向登錄頁。完成登錄功能。登錄之后更新導航,用上下文處理器定義函數,獲取session中保存的值,返回字典,在父模板中更新導航,插入登錄前發布器。完成注銷功能,清除session。發布功能的實現,制作首頁的顯示列表,首頁列表顯示全部問答,完成問答詳情頁布局,從首頁問答標題到問答詳情頁,完成評論功能,完成評論列表顯示及時間排序,個人中心顯示,個人中心標簽頁導航,完成個人中心—導航標簽,實現搜索功能(包括高級搜索即搜素題目和內容都可以出現),最重要的是實現密碼加密功能,在數據庫顯示密文,明文密碼只有個人用戶才清楚。
二、、Python+Flask+MysqL的web建設技術過程總結
1.使用工具:①pycharm64.exe ②Python 3.6 64-bit ③MySQL ④HEIDISQL
2.功能實現過程
導航:
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>{% block title %}{% endblock %}base</title> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="../static/css/shouye.css" type="text/css"> <script src="../static/js/shouye.js"></script> <manhua target="_blank"/> </head> {% block head %}{% endblock %} <body id="myBody"> <nav class="navbar navbar-default" role="navigation"> <img src="http://news.u17i.com/images/header_bg/header_bg_1508753122_9zUfjgQKJw7A.jpg"><br> <div class="container-fluid"> <div class="navbar-header"> <a href="http://127.0.0.1:5000/manhua"><img class="logo" id="tubiao" src="../static/images/dongman.jpg"></a> </div> <ul class="nav navbar-nav navbar-left" id="myBody"> <li><a href="{{ url_for('shouye') }}">首頁</a></li> <li><a href="http://127.0.0.1:5000/manhua">漫畫</a></li> <li><a href="{{ url_for('wenda') }}">問答</a></li> {% if name %} <li><a href="{{ url_for('usercenter',user_id=session.get('userid'),tag=1)}}">{{ session.get('user') }}</a></li> <li><a href="{{ url_for('logout') }}">注銷</a></li> {% else %} <li><a href="{{ url_for('login') }}">登陸</a></li> <li><a href="{{ url_for('register') }}">注冊</a></li> {% endif %} <li><img id="on_off" onclick="mySwitch()" src="../static/images/on.jpg" width="50px"></li> <form action="{{ url_for('search') }}" method="get" class="navbar-form navbar-right" style="float:right"> <input name="q" type="text" placeholder="請輸入搜索內容"> <input type="submit" value="搜索"> {# <input name="q" type="text" class="form-control" placeholder="請輸入關鍵字" >#} {# <button type="submit" class="btn btn-default"><span class="glyphicon glyphicon-search" aria-hidden="true" ></span>查找</button>#} </form> </ul> </div> </nav> {% block main %} {% endblock %} </body> </html>
漫畫頁面:
html
{% extends'shouye.html' %} {% block title %} manhua {% endblock %} {% block head %} <link rel="stylesheet" type="text/css" href="../static/css/manhua.css" type="text/css"> <script src="../static/js/manhua.js"></script> <manhua target="_blank"/> {% endblock %} {% block main %} <body id="myBody"> <div> <div class="katong"> <a href="https://baike.baidu.com/item/%E5%A4%A9%E7%A9%BA%E4%B9%8B%E5%9F%8E/6298?fr=aladdin" > <img src="https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike150%2C5%2C5%2C150%2C50/sign=bd7d3d834734970a537e187df4a3baad/50da81cb39dbb6fd47c8108a0f24ab18962b378e.jpg" width="300" height="200"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E5%A4%A9%E7%A9%BA%E4%B9%8B%E5%9F%8E/6298?fr=aladdin">天空之城</a></div> </div> <div class="katong"> <a href="https://baike.baidu.com/item/%E4%BD%A0%E7%9A%84%E5%90%8D%E5%AD%97%E3%80%82/19127928?fromtitle=%E4%BD%A0%E7%9A%84%E5%90%8D%E5%AD%97&fromid=19126915&fr=aladdin"> <img src="https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike220%2C5%2C5%2C220%2C73/sign=0000a3f3583d26973ade000f3492d99e/242dd42a2834349ba3d7f0bcc0ea15ce36d3be32.jpg" width="300" height="200"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E4%BD%A0%E7%9A%84%E5%90%8D%E5%AD%97%E3%80%82/19127928?fromtitle=%E4%BD%A0%E7%9A%84%E5%90%8D%E5%AD%97&fromid=19126915&fr=aladdin">你的名字</a></div> </div> <div class="katong"> <a href="https://baike.baidu.com/item/%E5%93%86%E5%95%A6A%E6%A2%A6%EF%BC%9A%E4%BC%B4%E6%88%91%E5%90%8C%E8%A1%8C/13845055?fr=aladdin"> <img src="https://gss0.bdstatic.com/-4o3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike220%2C5%2C5%2C220%2C73/sign=e1f375ca56da81cb5aeb8b9f330fbb73/aec379310a55b319a7591f4b44a98226cffc176f.jpg" width="300" height="200"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E5%93%86%E5%95%A6A%E6%A2%A6%EF%BC%9A%E4%BC%B4%E6%88%91%E5%90%8C%E8%A1%8C/13845055?fr=aladdin">哆啦A夢</a></div> </div> <div class="katong"> <a href="https://baike.baidu.com/item/%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85/8702?fr=aladdin"> <img src="http://www.285868.com/uploadfile/2016/1026/20161026032202862.jpg" width="300" height="200"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85/8702?fr=aladdin">火影忍者</a></div> </div> <div class="katong"> <a href="https://baike.baidu.com/item/%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85/8702?fr=aladdin"> <img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike80%2C5%2C5%2C80%2C26/sign=e7239edc02087bf469e15fbb93ba3c49/77c6a7efce1b9d16074c3bf6f3deb48f8d54649a.jpg" width="300" height="200"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E7%81%AB%E5%BD%B1%E5%BF%8D%E8%80%85/8702?fr=aladdin">吸血鬼騎士</a></div> </div> <div class="katong"> <a href="https://baike.baidu.com/item/%E7%96%AF%E7%8B%82%E5%8A%A8%E7%89%A9%E5%9F%8E/18869840?fr=aladdin"> <img src="https://gss0.bdstatic.com/94o3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D500/sign=6c3e4c6ad51b0ef468e8985eedc551a1/b64543a98226cffcd6ebbbf6be014a90f703eab5.jpg" width="300" height="200"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E7%96%AF%E7%8B%82%E5%8A%A8%E7%89%A9%E5%9F%8E/18869840?fr=aladdin">瘋狂動物城</a></div> </div> </div> <div class="katong"> <a href="https://baike.baidu.com/item/%E5%AF%BB%E6%A2%A6%E7%8E%AF%E6%B8%B8%E8%AE%B0/20483318?fr=aladdin"> <img src="https://gss1.bdstatic.com/-vo3dSag_xI4khGkpoWK1HF6hhy/baike/c0%3Dbaike116%2C5%2C5%2C116%2C38/sign=c48d88aa65061d95694b3f6a1a9d61b4/4e4a20a4462309f7fdde5c80790e0cf3d6cad6eb.jpg"width="300" height="200"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E5%AF%BB%E6%A2%A6%E7%8E%AF%E6%B8%B8%E8%AE%B0/20483318?fr=aladdin">尋夢環游記</a></div> </div> <div class="katong"> <a href="https://baike.baidu.com/item/%E8%88%AA%E6%B5%B7%E7%8E%8B/75861?fr=aladdin&fromid=8904&fromtitle=%E6%B5%B7%E8%B4%BC%E7%8E%8B"> <img src="https://gss2.bdstatic.com/9fo3dSag_xI4khGkpoWK1HF6hhy/baike/s%3D500/sign=d75e8d65df39b60049ce0fb7d9513526/9a504fc2d5628535274d5ca79aef76c6a6ef6383.jpg" width="300" height="200"></a> <div class="desc"><a href="https://baike.baidu.com/item/%E8%88%AA%E6%B5%B7%E7%8E%8B/75861?fr=aladdin&fromid=8904&fromtitle=%E6%B5%B7%E8%B4%BC%E7%8E%8B">海賊王</a></div> </div> </div> <div class="clearfloat"> <img src="http://p0.so.qhimgs1.com/t01b0bcf7da772c3e8c.jpg" width="500" height="500"> {% endblock %} </div> </body> </html>
實現開燈關燈功能:
js
function mySwitch() { var myele=document.getElementById("on_off") if (myele.src.match("on")) { myele.src="../static/images/off.jpg" document.getElementById("myBody").style.background="black" document.getElementById("demo").style.color="white" } else { myele.src="../static/images/on.jpg" document.getElementById("myBody").style.background="white" document.getElementById("demo").style.color="black" } }
登陸:
html
{% extends'shouye.html' %} {% block title %} Login {% endblock %} {% block head %} <link rel="stylesheet" type= "text/css" href="../static/css/denglu.css"> <script src="../static/js/denglu.js"></script> {% endblock %} {% block main %} <body> <div class="b1" > <form action="{{ url_for("login") }}" method="post"> <div class="s1" ><h3>用戶登錄/LOGIN </h3></div> <div class="b2" > 戶名:<input id="name" type="text"placeholder="請輸入用戶名"name="name"><br> 密碼:<input id="password" type="password"placeholder="請輸入密碼"name="password"><br> </div> <div id="error_box"><br></div> <div class="s2" > <button onclick="return myLogin()">登錄</button> <button type="button" onclick=window.alert("是否取消登錄")>取消</button> <label for="remember_me" style="padding: 0;">Remember me?</label> <input type="checkbox" id="remember_me" style="position: relative; top: 3px; margin: 0; " name="remember_me"/> <div class="design"> <p>Design by ss</p> </div> </div> </form> </div> </body> {% endblock %}
js
function myLogin() { var oUname = document.getElementById("name"); var oError = document.getElementById("error_box"); var opassword = document.getElementById("password"); oError.innerHTML="<br>"; //oUname if(oUname.value.length<6||oUname.value.length>12){ oError.innerHTML="用戶名請輸入6-12個字符"; return false; }else if ((oUname.value.charCodeAt(0)>=48) &&(oUname.value.charCodeAt(0)<=57)){ oError.innerHTML="用戶名首字符不能為數字"; return false; }else for(var i=0; i<oUname.value.length;i++){ if (oUname.value.charCodeAt(i)<48 || oUname.value.charCodeAt(i)>57 &&(oUname.value.charCodeAt(i) < 97) || (oUname.value.charCodeAt(i) > 122)){ oError.innerHTML="用戶名只能是字母和數字"; return false; } } //opassword if(opassword.value.length<6||opassword.value.length>12){ oError.innerText="密碼請輸入6-12個字符內"; return false; } return true; }
py
@app.route('/login',methods=['GET','POST']) def login(): if request.method =='GET': return render_template('denglu.html') else: name = request.form.get('name') passw = request.form.get('password') user = User.query.filter(User.username == name).first() if user: if user.check_password(passw): session['user'] = name session['userid'] = user.id session.permanent = True return redirect(url_for('manhua')) else: return "密碼錯誤!" else: return "用戶名不存在!"
導入包:
py
from flask import Flask, render_template, url_for, redirect, request,session from flask_sqlalchemy import SQLAlchemy from functools import wraps import config from datetime import datetime from sqlalchemy import or_ ,and_ from werkzeug.security import generate_password_hash, check_password_hash app = Flask(__name__) app.config.from_object(config) db = SQLAlchemy(app) class User(db.Model): __tablename__='user' id=db.Column(db.Integer,primary_key=True,autoincrement=True) username = db.Column(db.String(20), nullable=False) _password = db.Column(db.String(200), nullable=False) nickname = db.Column(db.String(50)) @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): result = check_password_hash(self._password, row_password) return result class Question(db.Model): __tablename__='question' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title =db.Column(db.String(100), nullable=False) detail = db.Column(db.Text, nullable=False) creat_time=db.Column(db.DateTime,default=datetime.now) author_id = db.Column(db.Integer,db.ForeignKey('user.id')) author=db.relationship('User',backref=db.backref('question')) question_id = db.Column(db.Integer,db.ForeignKey('question.id')) class Comment(db.Model): __tablename__ = 'comment' id = db.Column(db.Integer, primary_key=True, autoincrement=True) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) question_id = db.Column(db.Integer, db.ForeignKey('question.id')) create_time = db.Column(db.DateTime, default=datetime.now) detail = db.Column(db.Text, nullable=False) question = db.relationship('Question', backref=db.backref('comments')) author = db.relationship('User', backref=db.backref('comments'))
注冊:
html
{% extends'shouye.html' %} {% block title %} register {% endblock %} {% block head %} <link rel="stylesheet"type="text/css"href="../static/css/zhuce.css"> <script src="../static/js/zhuce.js"></script> {% endblock %} {% block main %} <body> <div class="b1" > <form action="{{ url_for("register") }}" method="post"> <div class="s1" ><h3>注冊/ENROLL </h3></div> <div class="b2" > 請輸入賬號: <input id="name" type="text" placeholder="write down your name" name="name"><br> 請輸入密碼: <input id="password" type="password" placeholder="write down your PIN" name="password"><br> 再輸入密碼: <input id="password2" type="password" placeholder="write down your PIN" name="password2"><br> </div> <div id="error_box"><br></div> <div class="s2" > <button onclick="return myLogin()">注冊</button> <button type="button" onclick=window.alert("是否取消注冊")>取消</button> <label for="remember_me" style="padding: 0;">Remember me?</label> <input type="checkbox" id="remember_me" style="position: relative; top: 3px; margin: 0; " name="remember_me"/> <div class="design"> <p>Design by ss</p> </div> </div> </form> </div> </body> {% endblock %}
js
function myLogin() { var oUname = document.getElementById("name") var oError = document.getElementById("error_box") var opassword = document.getElementById("password") var opassword2 = document.getElementById("password2") oError.innerHTML = "<br>" //oUname if (oUname.value.length < 6 || oUname.value.length > 12) { oError.innerHTML = "用戶名請輸入6-12個字符"; return false; } else if ((oUname.value.charCodeAt(0) >= 48) && (oUname.value.charCodeAt(0) <= 57)) { oError.innerHTML = "用戶名首字符不能為數字"; return false; } else for (var i = 0; i < oUname.value.length; i++) { if (oUname.value.charCodeAt(i) < 48 || oUname.value.charCodeAt(i) > 57 && (oUname.value.charCodeAt(i) < 97) || (oUname.value.charCodeAt(i) > 122)) { oError.innerHTML = "用戶名只能包含字母和數字"; return false; } } //opassword if (opassword.value.length < 6 || opassword.value.length > 20) { oError.innerHTML = "密碼請輸入6-12個字符內"; return false; } if (opassword2.value != opassword.value) { oError.innerHTML = "登陸請重新輸入你的密碼"; return false; } window.alert("注冊成功!") return true; }
py
@app.route("/register",methods=['GET','POST']) def register(): if request.method == 'GET': return render_template("zhuce.html") else: name = request.form.get('name') passw = request.form.get('password') user = User.query.filter(User.username == name).first() if user: return "用戶名已存在!" else: user = User(username=name, password=passw) db.session.add(user) db.session.commit() return render_template("denglu.html")
登陸后顯示登陸名,注冊變為注銷:
html
{% if name %} <li><a href="{{ url_for('usercenter',user_id=session.get('userid'),tag=1)}}">{{ session.get('user') }}</a></li> <li><a href="{{ url_for('logout') }}">注銷</a></li> {% else %} <li><a href="{{ url_for('login') }}">登陸</a></li> <li><a href="{{ url_for('register') }}">注冊</a></li> {% endif %} <li><img id="on_off" onclick="mySwitch()" src="../static/images/on.jpg" width="50px"></li> <form action="{{ url_for('search') }}" method="get" class="navbar-form navbar-right" style="float:right"> <input name="q" type="text" placeholder="請輸入搜索內容"> <input type="submit" value="搜索">
py
@app.route('/logout') def logout(): session.clear() return redirect(url_for('manhua'))
問答
html
{% extends'shouye.html' %} {% block title %} wenda {% endblock %} {% block head %} <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="../static/css/wenda.css"> {% endblock %} {% block main %} <body> <div class="form-group"> <h1 style="color:blueviolet", align="center"> 漫畫問卷調查</h1> <form action="{{ url_for("wenda") }}" method="post"> <div class="q"> <label for="question">電影</label><br> <textarea id="question" cols="100" rows="1" name="title"></textarea> </div> <div class="form-group"> <label for="questionDetail">反饋</label> <textarea class="form-control" id="questionDetail" cols="50" rows="5" name="detail"></textarea> </div> <br> <div class="input-area"> <button onclick="fnQuestion">發布</button> </div> </form> </div> {% endblock %} </body>
py
@app.route('/wenda',methods=['GET','POST']) @loginFirst def wenda(): if request.method=='GET': return render_template('wenda.html') else: title=request.form.get('title') detail=request.form.get('detail') author_id=User.query.filter(User.username==session.get('user')).first().id question=Question(title=title,detail=detail,author_id=author_id) db.session.add(question) # 數據庫,添加操作 db.session.commit() return redirect(url_for('manhua'))
首頁顯示發布問答
html
{% extends 'shouye.html' %} {% block title %}首頁{% endblock %} {% block head %} <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="../static/css/sywenda.css"> {% endblock %} {% block main %} <div class="label"> <ul class="list-group"> {% for foo in questions %} <li class="list-group-item"> <div class="s1"> <a href="{{ url_for('sywdxq',question_id=foo.id )}}">電影名稱:{{ foo.title }}</a><br> <a href="#">反饋詳情:{{ foo.detail }}</a><br> </div> <a class="s" href="{{ url_for('usercenter',user_id=foo.author.id,tag = 1) }}">{{ foo.author.username }}</a><br> <span class="badge" style="margin-left: 60%">{{ foo.creat_time }}發布時間</span> <p style="margin-left: 25%"></p> </li> {% endfor %} </ul> </div> {% endblock %}
首頁問答詳情及評論:
{% extends 'shouye.html' %} {% block title %}首頁{% endblock %} {% block head %} <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="../static/css/sywdxq.css"> {% endblock %} {% block main %} <div class="col-md-2 column "></div> <div class="col-md-8 column "> <div class="page-headr"> <h1>welcome !!!</h1><br> <h3>Title:{{ ques.title }}<br> <a class="ss" href="{{ url_for('usercenter',user_id=ques.author.id,tag = 1) }}">{{ ques.author.username }}</a><br> {# <small class="ss" href="{{ url_for('usercenter',user_id=ques.author.id,tag = 1) }}" >author:{{ ques.author.username }}#} <br><span class="badge">發布時間:{{ ques.creat_time }}</span></small> </h3> <hr> <p>detail:{{ ques.detail }}</p> <hr> <form action="{{url_for('comment')}}" method="post" style=""> <div class="form-group"> <textarea name="new_comment" class="form-control" rows="5" id="new-comment" placeholder="write your comments" style="width: 800px"></textarea><br> <input name="question_id" type="hidden" value="{{ ques.id }}"> </div> <br> <button type="submit" class="btn btn-default" style="width:100px "> 發送</button> </form> <ul class="list-group" style="margin: 0px"></ul> </div> </div> {# <div class="col-md-2 column ">#} <h4>評論:({{ ques.comments|length }})</h4> <ul class="list"> {% for foo in ques.comments %} <li class="post_item"> <a href="{{ url_for('usercenter',user_id=foo.author.id,tag=1) }}" class="light">{{foo.author.username }}</a><br> {# <img width="48" height="48" class="pf" src="https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4066191998,32277144&fm=27&gp=0.jpg" alt="">#} <p class="post_item">{{ foo.detail }}</p> <span class="foot">發布於 {{ foo.create_time }}</span> </li> <hr> {% endfor %} </ul> {% endblock %}
py
@app.route('/sywdxq/<question_id>') def sywdxq(question_id): quest = Question.query.filter(Question.id == question_id).first() return render_template("sywdxq.html",ques=quest) def loginFirst(func): #行動前需要登錄,定義裝飾器 @wraps(func) def wrapper(*args,**kwargs): if session.get('user'): return func(*args,**kwargs) else: return redirect(url_for('login')) return wrapper @app.route('/comment/',methods=['POST']) @loginFirst def comment(): comment=request.form.get('new_comment') ques_id=request.form.get('question_id') auth_id=User.query.filter(User.username==session.get('user')).first().id comm = Comment(author_id=auth_id,question_id=ques_id,detail=comment) db.session.add(comm) # 數據庫,添加操作 db.session.commit() return redirect(url_for('sywdxq',question_id=ques_id))
用戶詳情:
問答、評論、個人信息html
{% extends 'userbase.html' %} {% block title %}個人中心{% endblock %} {% block user %} <div class="page-header"> <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span>{{ username }}<br> <small>全部問答<span class="badge"></span></small> </h3> <ul class="list-group" style="margin: 10px"> {% for ques in question %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span> <a href="#">{{ ques.author.username }}</a> <span class="badge">{{ ques.create_time }}</span> <p>{{ ques.detail }}</p> </li> {% endfor %} </ul> </div> {% endblock %}
{% extends 'userbase.html' %} {% block user %} <div class="page-header"> <br> <h3><small>全部評論<span class="badge"></span></small></h3> <ul class="list-group" style="margin: 10px"> {% for ques in comments %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span> <a href="#">{{ ques.author.username }}</a> <span class="badge">{{ ques.create_time }}</span> <p>{{ ques.detail }}</p> </li> {% endfor %} </ul> </div> {% endblock %}
{% extends 'userbase.html' %} {% block user %} <div class="page-header"> <br> <h3><small>個人信息<span class="badge"></span></small></h3> <ul class="list-group" style=" "> <li class="list-group-item">用戶:{{ username }}</li> <li class="list-group-item">評論:{{ comments|length }}</li> <li class="list-group-item">文章數:{{ question|length }}</li> <p> </p> </ul> </div> {% endblock %}
{% extends 'shouye.html' %} {% block title %}個人中心{% endblock %} {% block head %} <style> .nav_ul li { list-style:none; float:left; margin:10px; border-bottom:outset; } </style> {% endblock %} {% block main %} <ul class="nav_ul"> <li role="presentation"><a href="{{ url_for('usercenter', user_id = user.id ,tag="1") }}">全部問答</a></li> <li role="presentation"><a href="{{ url_for('usercenter',user_id = user.id,tag="2") }}">全部評論</a></li> <li role="presentation"><a href="{{ url_for('usercenter',user_id = user.id,tag="3") }}">個人信息</a></li> </ul> {% block user %}{% endblock %} {% endblock %}
個人中心
html
{% extends 'shouye.html' %} {% block title %}個人中心{% endblock %} {% block head %} <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link rel="stylesheet" type="text/css" href="../static/css/usercenter.css"> {% endblock %} {% block main %} <div class="page-header"> <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span>{{ username }}<br> <small>全部問答<span class="s1"></span></small> </h3> <ul class="list-group" style="margin: 10px"> {% for foo in question %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span> <a href="#">{{ foo.author.username }}</a> <span class="s2">{{ foo.create_time }}</span> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div class="page-header"> <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span>{{ user }}<br> <small>全部評論<span class="s1"></span></small> </h3> <ul class="list-group" style="margin: 10px"> {% for foo in comments %} <li class="list-group-item"> <span class="glyphicon glyphicon-heart-empty" aria-hidden="true"></span> <a href="#">{{ foo.author.username }}</a> <span class="s2">{{ foo.create_time }}</span> <p>{{ foo.detail }}</p> </li> {% endfor %} </ul> </div> <div class="page-header"> <h3><span class="glyphicon glyphicon-user" aria-hidden="true"></span>{{ user }}<br> <small>個人信息<span class="s1"></span></small> </h3> <ul class="list-group" style="margin: 10px"> <li class="list-group-item">用戶:{{ username }}</li> <li class="list-group-item">編號:</li> <li class="list-group-item">昵稱:</li> </ul> </div> {% endblock %}
數據庫密碼加密:
py
class User(db.Model):
__tablename__='user'
id=db.Column(db.Integer,primary_key=True,autoincrement=True)
username = db.Column(db.String(20), nullable=False)
_password = db.Column(db.String(200), nullable=False)
nickname = db.Column(db.String(50))
@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):
result = check_password_hash(self._password, row_password)
return result
謝謝老師一學期以來的指導,雖然我還有很多不足,在今后我會繼續努力做的更好!