在前期Python的基礎課程中學習到了turtle庫的基礎練習、字符串的基本操做、凱撒密碼 、GDP格式化輸出、99乘法表、中英文詞頻統計、和datetime處理日期和時間等等基礎學習。html
其中最有趣的也是最簡單的就是turtle庫的基礎練習,畫出五角星,比較好玩,在後來網頁製做的課程中我學習各類網頁前端頁面的效果,還有後臺鏈接數據庫,並學會在網頁上利用pysql庫代碼對數據庫的數據進行增刪查改,在頁面顯示效果。前端
最後作出了一個較爲粗糙的論壇網站,模仿了老師上課講的,看了截圖,請教了同窗才作出一個簡單的網站。java
在從此還會繼續學習Python的。我剛剛開始接觸到Python這門編程語言的時候,就發現相比以往學習的java這門編程語言中,這門語言更容易入門,代碼較爲精簡,而功能很是齊全,不止能夠用來製做網頁,並且能夠進行數據挖掘,而我在學習中能夠很快的上手,而且比較有興趣的去打代碼,不像以往打JAVA代碼同樣很是的煩悶,能夠較爲輕鬆的心態去作好Python。python
有一句程序員們調侃的一句話,叫:」人生苦短,我學Python」。程序員
項目自己的python代碼就很少,可是功能卻十分完善,能夠很好地上面那一句調侃的話。算法
同窗們也能夠在閒的時候來個人平臺上發佈一些評論和見解,你們一塊兒多多交流一下,咱們的學習狀況,還有生活。sql
系統採用Python的Flask輕量級服務器端的框架,而且使用的是jinja2模板引擎作到了先後臺代碼分離。Bootstrap下的DIV+CSS開發模式。數據庫
推薦文章編程 |
評論安全 |
點贊 |
收藏 |
評論分享網 |
首頁 |
文章詳情 |
我的中心 |
文章發佈 |
熱門文章 |
文章列表 |
高級搜索 |
文章分類 |
註冊 |
登錄 |
上傳頭像 |
修改密碼 |
個人收藏 |
個人發佈 |
個人評論 |
分類 |
網站的結構就是經過使用結構化的方法,對網頁中所需的信息進行整理以及是分類,使得網站的內容更具備條理性、邏輯性和清晰性。相信不少網站建設者在改版網站的時候,都會發現不少網站都沒有本身的結構格局,一打開網站的頁面就像一團佈滿摺痕的廢紙同樣,用戶是很難在裏面找到本身想要的信息的。這就比如讀者在打開一本小說,忽然發現裏面沒有了段落以及標點,並且字間也沒有間隙,筆者相信不出幾秒,讀者一定會以爲頭暈眼花。因此一個好的結構是頁帶來好的用戶體驗的重要的一環。
網站結構的表現則是經過技術對已經被構化的信息進行顯示上的控制,好比說版式、顏色小等樣式的控制。表現是結構的一種昇華,僅僅只是有合理的條理是不夠的,還要作到讓人賞心悅目,讓別人更加喜歡網站,給人一種眼前一 亮的感受。一本好書除了要有好的內容外,還有點很重要的是能吸引人的眼球,別人第一眼喜歡上了你的表現,才能進一步的對的內容產生興趣。
本站在設計上就採用精簡,功能明確,不須要過多的冗餘。
詳情頁是指文章點進去後,顯示文章內容的頁面,其中還包括如下:
1) 推薦文章。推薦你喜歡的文章。
2) 評論。對這篇文章你的見解是如何的。
3) 點贊。以爲寫的好,就點個贊吧。
4) 收藏。把你認爲好的,之後還會看收藏起來,在個人我的中心,之後還能夠點開來看
首頁主要包括:
1) 熱門文章。給用戶推薦平臺上,點擊數量最大的前五本書,讓用戶能夠了解你們都喜歡什麼樣的書。
2) 文章列表。每一本書的標題、時間、做者、分享還有簡介的摘要也會以一條條的形式顯示在首頁,並且每一秒都會顯示再新的分享。
3) 高級搜索。單一的搜索只能找到書中有過的關鍵字,高級搜索能夠將多個條件同時錄入進行搜索。
4) 文章分類。以類別分別顯示文章列表。
5) 註冊。新用戶註冊平臺帳號。
6) 登錄。登陸帳號去管理我的中心中的我的信息。
對於有以登陸的用戶,平臺還提供了我的中心去管理本身的信息:
1) 上傳頭像。用戶能夠自定義本身的頭像,默認是一個黑色的頭像,上傳有本身個性的頭像,可讓別人更加容易記得本身。
2) 修改密碼。爲了帳號安全,按期修改密碼。
3) 個人收藏。把你認爲好的,之後還會看收藏起來,在個人我的中心,之後還能夠點開來看。
# 退出
@app.route('/logout/')
def logout():
session.pop('username')
return redirect(url_for('index'))
# 上下文
@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 {}
# 登錄
@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 redirect(url_for('login'))
else:
return redirect(url_for('login'))
# 註冊
@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 'error:user exitst'
else:
user = User(username=username, password=password)
db.session.add(user) # 加入數據庫
db.session.commit()
return redirect(url_for('login'))
# 定義一個裝飾器出驗證用戶有是不是登錄
# 定義一個參數函數
def loginFirst(func):
# 定義一個函數將其返回
@wraps(func)
def wrapper(*args, **kwargs):
if session.get('username'):
return func(*args, **kwargs)
else:
return redirect(url_for('login'))
# 返回一個函數
return wrapper
#修改密碼
@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))
# 發佈問答
@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'))
# 發佈評論
@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))
# 首頁
@app.route('/')
def index():
if request.args.get('info'):
info = request.args.get('info')
else:
info = None;
context = {
'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', **context)
# 某用戶發佈過的全部評論
@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)
#分類顯示
@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)
# 詳情頁
@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
db.session.commit()
content = {
'ques': quest,
'comment': comment,
'col': col,
'questions': Question.query.filter(Question.cf == quest.cf).all(),
}
return render_template('detail.html', **content)
頭像地址 |
註冊日期 |
收藏 |
日期 |
評論 |
用戶 |
編號 |
密碼 |
顯示名稱 |
登陸帳號名 |
分類 |
分類編號 |
名稱 |
編號 |
問題 |
標題 |
發佈時間 |
發佈者 |
內容 |
頭像 |
閱讀數 |
點擊數 |
日期 |
內容 |
# 收藏表
Collection = db.Table(
'collection',
db.Column('id', db.Integer, primary_key=True, autoincrement=True),
db.Column('book_id', db.Integer, db.ForeignKey('question.id')), # 評論對應的文章的id
db.Column('collection', db.Integer, db.ForeignKey('user.id')), # 收藏用戶的id
db.Column('createdate', db.DATETIME) # 發佈時間
)
#用戶表
class User(db.Model):
__tablename__ = 'user'
# 創建一個表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)
say = db.Column(db.String(50))
icon = db.Column(db.String(50), default='uploads/default_logo.jpg')
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)
#評論表
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'))
creat_time = db.Column(db.DateTime, default=datetime.now())
detail = db.Column(db.TEXT, nullable=False)
question = db.relationship('Question', backref=db.backref('comment'))
author = db.relationship('User', backref=db.backref('comment', order_by=creat_time.desc))
#問題表
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'))
cf = db.Column(db.Integer, db.ForeignKey('cf.id'))
look = db.Column(db.Integer, default=0)
click = db.Column(db.Integer, default=0)
author = db.relationship('User', backref=db.backref('question'))
cfClass = db.relationship('Cf', backref=db.backref('question'))
# 分類
class Cf(db.Model):
__tablname__ = 'cf'
id = db.Column(db.Integer, primary_key=True, autoincrement=True) # 數據庫惟識別id
name = db.Column(db.String(30)) # 文章名稱
context = db.Column(db.TEXT) # 分類內容
# 上傳頭像
@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)); 5.2 前面用戶帳號密碼檢
# 模糊查找
@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()
context = {
'questions': query,
'cf': Cf.query.all(),
'hot': Question.query.order_by('-click').all()[0:5]
}
return render_template('index.html', **context)
前端代碼:
<form class="navbar-form navbar-left" role="search" action="{{ url_for('search') }}" method="get">
<div class="form-group">
<input type="text" class="form-control" placeholder="搜索" required name="q">
</div>
<div class="form-group">
<select class="form-control" name="c">
<option value="" selected>--分類--</option>
{% for c in cf %}
<option value="{{ c.id }}">{{ c.name }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<select class="form-control" name="y">
<option value="" selected>--發佈年份--</option>
<option>2018</option>
<option>2017</option>
<option>2016</option>
<option>2015</option>
<option>2014</option>
</select>
</div>
<button type="submit" class="btn btn-default">搜索</button>
</form>
網站父模板統一佈局
這些頁面所有繼承與父模板,導航就是父模板中的
註冊、登陸、註銷
發佈、列表顯示
詳情頁
評論、列表顯示
我的中心
高級搜索
文章分類、顯示
點贊、收藏
修改密碼、頭像、上傳頭像
熱門文章、推薦文章