import json
from functools import wraps
from . import *
from flask import render_template, request, flash, redirect, url_for, make_response, session,escape
from .forms import RegisterForm,ArticleForm
from passlib.hash import sha256_crypt
from .models import Users,Artciles
@app.route('/register',methods=['GET','POST'])
def register():
form = RegisterForm(request.form)
if form.validate_on_submit():
# 接收表单数据
email = form.email.data
username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))
# 存入数据库
user = Users()
user.email =email
user.username = username
user.password = password
db.session.add(user)
db.session.commit()
flash('您已注册成功,请登录','success')
return redirect(url_for('login'))
return render_template('register.html',form=form)
@app.route('/login',methods=['GET','POST'])
def login():
# 查看是否已经登录,若已登录,直接跳转
if 'login_in' in request.cookies:
return redirect(url_for('dashboard'))
else:
if 'login_in' in session:
session['login_in'] = True
session['username'] = request.cookies.get('username')
return redirect(url_for('dashboard'))
if request.method == 'POST':
# 获取表单数据
username = request.form['username']
password_candicate = request.form['password']
res = Users.query.filter_by(username=username).first()
if res:
password = res.password
if sha256_crypt.verify(password_candicate,password):
# 调用verify方法验证如果为真,验证通过
# 写入cookie
response = make_response(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n'
"<title>Redirecting...</title>\n"
"<h1>Redirecting...</h1>\n"
"<p>You should be redirected automatically to target URL: "
'<a href="%s">%s</a>. If not click the link.'
% (escape(url_for('dashboard')),
escape(url_for('dashboard'))), 302)
response.set_cookie('login_in','True',max_age=60*60*24*38)
response.set_cookie('username',username,max_age=60*60*24*38)
# 存入session
session['login_in'] = True
session['username']=username
response.location = escape(url_for('dashboard'))
return response
else:
error = '用户名和密码不匹配'
return render_template('login.html',error=error)
else:
error = '用户名不存在'
return render_template('login.html',error=error)
return render_template('login.html')
def is_login_in(f):
@wraps(f)
def wrap(*args,**kwargs):
if 'login_in' in session:
return f(*args,**kwargs)
else:
flash('无权访问,请先登录','danger')
return redirect(url_for('login'))
return wrap
@app.route('/logout')
@is_login_in
def logout():
if 'login_in' in request.cookies:
response = make_response(
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n'
"<title>Redirecting...</title>\n"
"<h1>Redirecting...</h1>\n"
"<p>You should be redirected automatically to target URL: "
'<a href="%s">%s</a>. If not click the link.'
% (escape(url_for('index')),
escape(url_for('index'))), 302)
response.delete_cookie('login_in')
response.location = escape(url_for('index'))
return response
else:
session.clear()
flash('您已成功退出', 'success')
return redirect(url_for('index'))
@app.route('/dashboard')
@is_login_in
def dashboard():
notes = Artciles.query.order_by(Artciles.timestamp.desc()).all()
if notes:
return render_template('dashboard.html',notes=notes)
else:
msg = '暂无笔记信息'
return render_template('dashboard.html',msg=msg)
@app.route('/artcile/<string:id>')
@is_login_in
def artcile(id):
note = Artciles.query.get(id)
return render_template('article.html',note=note)
@app.route('/add_article',methods=['GET','POST'])
@is_login_in
def add_article():
form = ArticleForm()
if form.validate_on_submit():
note = Artciles()
note.title = form.title.data
note.author = session['username']
note.body = form.body.data
db.session.add(note)
db.session.commit()
flash('提交成功','success')
return redirect(url_for('dashboard'))
return render_template('add_article.html', form=form) # 渲染模板
@app.route('/delete_article/<int:id>',methods=['POST'])
@is_login_in
def delete_article(id):
note = Artciles.query.filter_by(id=id).first()
print(note.body)
db.session.delete(note)
db.session.commit()
flash('删除成功','success')
return redirect(url_for('dashboard'))
@app.route('/edit_article/<int:id>',methods=['GET','POST'])
@is_login_in
def edit_article(id):
note = Artciles.query.filter_by(id=id).first()
if not note:
flash('ID错误','danger')
return redirect(url_for('dashboard'))
form = ArticleForm(request.form)
if form.validate_on_submit():
note.title = form.title.data
note.body = form.body.data
db.session.add(note)
db.session.commit()
flash('更改成功','success')
return redirect(url_for('dashboard'))
form.title.data = note.title
form.body.data = note.body
return render_template('edit_article.html',form=form)
@app.route('/')
def index():
return render_template('home.html')
# 如果表单提交且字段验证通过,查询数据库是否存在相同数据
# 如果存在,返回错误提示,如果不存在,则存入数据库
@app.route('/check_info')
def check_info(data):
# 接收数据
email = data
if Users.object.filter(email=email):
dic = {
'status':0,
'msg':'邮箱已被注册'
}
else:
dic = {
'status': 1,
'msg': '邮箱可以使用'
}
return json.dumps(dic)
@app.route('/checkLogged')
def checkLogged():
if 'login_in' in session:
dic = {
'loginStatus':1,
}
else:
dic = {
'loginStatus':0,
}
print(dic['loginStatus'])
return json.dumps(dic)
if __name__ == '__main__':
app.run(debug=True)
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
【资源说明】 基于Python+Flask框架开发的在线笔记系统设计与实现-毕业设计源码+使用文档(高分优秀项目).zip基于Python+Flask框架开发的在线笔记系统设计与实现-毕业设计源码+使用文档(高分优秀项目).zip 【备注】 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如软件工程、计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载使用,也可作为毕设项目、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 3、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
资源推荐
资源详情
资源评论
收起资源包目录
基于Python+Flask框架开发的在线笔记-毕业设计源码+使用文档(高分优秀项目).zip (46个子文件)
Notes-master
Notes
__init__.py 369B
venv
pyvenv.cfg 124B
Scripts
python.exe 511KB
deactivate.bat 368B
pythonw.exe 510KB
activate.bat 986B
Activate.ps1 1KB
activate 2KB
app.py 7KB
models.py 886B
templates
register.html 955B
includes
_navbar.html 1KB
_formhelpers.html 250B
_messages.html 393B
_footer.html 294B
articles.html 579B
article.html 283B
dashboard.html 966B
home.html 386B
login.html 1KB
layout.html 737B
edit_article.html 672B
about.html 237B
add_article.html 679B
.idea
workspace.xml 9KB
misc.xml 302B
Notes.iml 748B
inspectionProfiles
profiles_settings.xml 174B
modules.xml 269B
__pycache__
forms.cpython-37.pyc 1KB
app.cpython-37.pyc 5KB
models.cpython-37.pyc 888B
__init__.cpython-37.pyc 506B
test.py 403B
static
注销drawio.png 10KB
更新笔记流程图.png 15KB
js
login.js 544B
register.js 845B
navbar.js 537B
注册流程图.png 21KB
创建文章流程图.png 18KB
登录流程图.png 61KB
css
style.css 420B
删除笔记流程图.png 20KB
forms.py 1KB
171265889347208773632.zip 416B
共 46 条
- 1
资源评论
不走小道
- 粉丝: 3199
- 资源: 4969
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功