期末作品检查
Posted Marsk
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了期末作品检查相关的知识,希望对你有一定的参考价值。
-
个人学期总结
- 本学期学*了*几年比较热门的python,发现python相对Java要简单也有趣很多,完成同一个任务,C语言要写1000行代码,Java只需要写100行,而Python可能只要20行。学*python比较容易入门。Python具有面向对象,解释型,丰富和强大的库,优雅,明确,简单等特点。当然也存在运行速度慢和不能加密的缺点。课程分为两部分,前一部分主要是python的基本操作,包括输入输出,基本数据类型,运算,turtle库,循环,条件,函数定义,字符串操作,词频统计等等。后一部分才是重难点,主要是web开发基础,需要我们熟悉掌握网页结构,html基础,CSS基础,javascript基础,链接到数据库等等。本次网页设计将这一学期所学的web开发基础的知识用到了具体的实践中去,深化了理论知识,同时也锻炼了动手实践能力。在这一个学期里,我查阅了许多有关网页设计的相关的资料,结合自己的经验,网页设计中出现许多小问题。但是由于不细心在具体实践中却浪费了不少时间,看来平时还是要多多实践才是。通过本次网页设计我锻炼了自己的实践能力,通过实践我也深刻的体会到网页设计的艰辛及问题解决后的喜悦心情,培养我的独立思考问题的能力,同时也增强了我的理论联系实际的能力,这为自己以后的工作奠定了良好的基础。本网页设计还存在许多的缺陷和不足之处,比如很多细节上做的还不行,画面比较简约,有些功能模块还应再加强。希望在以后的时间里,我可以把这些缺陷都弥补过来,进一步完善网页设计。
- 总结Python+Flask+mysql的web建设技术过程,标准如下:
- 即是对自己所学知识的梳理
- 也可作为初学入门者的简单教程
- 也可作为自己以后复*的向导
- 也是一种向外展示能力的途径
开始flask项目
建立mysql和app的连接
创建用户模型
from flask import Flask from flask_sqlalchemy import SQLAlchemy import config from flask import Flask, render_template, request, redirect, url_for,session from functools import wraps from datetime import datetime from sqlalchemy import or_, and_ 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(20), nullable=False) nickname = db.Column(db.String(20)) db.create_all() 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(\'questions\')) db.create_all() 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\')) detail = db.Column(db.Text, nullable=False) creat_time = db.Column(db.DateTime, default=datetime.now) question=db.relationship(\'Question\',backref=db.backref(\'comments\',order_by=creat_time.desc)) author = db.relationship(\'User\', backref=db.backref(\'comments\')) db.create_all() @app.route(\'/\') def base(): return render_template(\'base.html\') @app.route(\'/index/\') def index(): context = { \'questions\':Question.query.order_by(\'-creat_time\').all() } return render_template(\'index.html\',**context) @app.route(\'/login/\',methods=[\'GET\',\'POST\']) def login(): if request.method == \'GET\': return render_template(\'login.html\') else: usern = request.form.get(\'username\') passw = request.form.get(\'password\') user = User.query.filter(User.username == usern).first() if user: if user.password==passw: session[\'user\'] = usern session[\'userid\'] = user.id session.permanent = True return redirect(url_for(\'base\')) else: return u\'password error\' else: return u\'password is not existed\' @app.route(\'/regist/\',methods=[\'GET\',\'POST\']) def regist(): if request.method==\'GET\': return render_template(\'regist.html\') else: usern=request.form.get(\'username\') passw=request.form.get(\'password\') nickn=request.form.get(\'nickname\') user=User.query.filter(User.username==usern).first() if user: return u\'username existed\' else: user=User (username=usern,password=passw,nickname=nickn) db.session.add(user) db.session.commit() return redirect(url_for(\'login\')) 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(\'/question/\',methods=[\'GET\',\'POST\']) @loginFirst def question(): if request.method==\'GET\': return render_template(\'question.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(\'index\')) @app.route(\'/detail/<question_id>\') def detail(question_id): quest = Question.query.filter(Question.id == question_id).first() return render_template(\'detail.html\', ques=quest)
@app.route(\'/comment/\',methods=[\'POST\']) @loginFirst def comment(): detail = request.form.get(\'fabu\') author_id = User.query.filter(User.username == session.get(\'user\')).first().id quest_id =request.form.get(\'question_id\') pinglun = Comment(question_id=quest_id,detail=detail, author_id=author_id) db.session.add(pinglun) db.session.commit() return redirect(url_for(\'detail\',question_id=quest_id)) @app.route(\'/usercenter/<user_id>/<tag>\') @loginFirst def usercenter(user_id,tag): user=User.query.filter(User.id== user_id).first() context={ \'user\':user } if tag==\'1\': return render_template(\'user1.html\',**context) elif tag==\'2\': return render_template(\'user2.html\',**context) else: return render_template(\'user3.html\',**context) @app.context_processor def mycontext(): usern=session.get(\'user\') if usern: return{\'username\':usern} else: return{} @app.route(\'/search/\') def search(): qu=request.args.get(\'q\') ques=Question.query.filter(or_(Question.title.contains(qu),Question.detail.contains(qu))).order_by(\'-creat_time\') return render_template(\'index.html\', questions=ques) @app.route(\'/logout/\') def logout(): session.clear() return redirect(url_for(\'index\')) if __name__ == \'__main__\': app.run(debug=True)
数据库配置信息config.py
import os SECRET_KEY = os.urandom(24) SQLALCHEMY_DATABASE_URI = \'mysql+pymysql://root:569472176@127.0.0.1:3306/mis_db?charset=utf8\' SQLALCHEMY_TRACK_MODIFICATIONS = False
父模板
<!DOCTYPE HTML> <html> <head lang="en"> <meta charset="UTF-8"> <title>{% block title %}首页{% endblock %}</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"> {% block head %}{% endblock %} <body id="mybody"> <nav class="navbar navbar-expand-lg navbar navbar-dark bg-dark"style="opacity:0.8;"> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <ul class="navbar-nav mr-auto"> <li class="nav-item"><a class="navbar-brand" href="{{ url_for(\'index\') }}">首页 </a></li> <li class="nav-item active"><a class="navbar-brand" href="{{ url_for(\'question\') }}">发布问答 </a></li> <li class="nav-item active"><a class="navbar-brand" href="https://www.dji.com/cn/products/consumer?site=brandsite&from=nav">消费级产品</a></li> <li class="nav-item active"><a class="navbar-brand" href="https://www.dji.com/cn/products/professional?site=brandsite&from=nav">专业级产品</a></li> <li class="nav-item active"><a class="navbar-brand" href="https://www.dji.com/cn/support?site=brandsite&from=nav">服务与支持</a></li> <form action="{{url_for(\'search\')}}" method="get" class="form-inline" > <input name="q" class="form-control mr-sm-2" type="search" placeholder="请输入关键字"style="border-radius: 25px" > <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> </form> </ul> <nav class="navbar navbar-expand-lg navbar navbar-dark bg-dark"> {% if username %} <a class="navbar-brand" href="{{ url_for(\'usercenter\',user_id=session.get(\'userid\'),tag=1) }}">{{ session.get(\'user\') }} </a> <a class="navbar-brand" href="{{ url_for(\'logout\') }}">注销 </a> {% else %} <a class="navbar-brand" href="{{ url_for(\'login\') }}">登陆 </a> <a class="navbar-brand" href="{{ url_for(\'regist\') }}">注册 </a> {% endif %} </nav> </div> </nav> <body background="https://stormsend1.djicdn.com/stormsend/uploads/75603600-92eb-0135-c4b0-12528100fbe3/P4P_.jpg"style="background-repeat:no-repeat;background-size: 100% 100%; background-attachment: fixed;"> {% block main %} {% endblock %} <div id="footer"> <div class="footerNav" style="text-align: center;position:fixed; left:0px; bottom:0px; width:100%; height:50px; z-index:9999"> <a rel="nofollow" href="https://www.dji.com/cn/company?site=brandsite&from=footer">关于我们</a> | <a rel="nofollow" href="https://www.dji.com/cn/terms?site=brandsite&from=footer">服务条款</a> | <a rel="nofollow" href="https://www.dji.com/cn/sitemap?site=brandsite&from=footer">网站地图</a> | <a rel="nofollow" href="https://www.dji.com/cn/contact?site=brandsite&from=footer">联系我们</a> </div> <div class="copyRight" style="text-align: center;position:fixed; left:0px; bottom:0px; width:100%; height:30px; z-index:9999"> Copyright ©Mars版权所有 </div> </div> </body> </body> </html>
首页列表显示全部问答:
- 将数据库查询结果传递到前端页面 Question.query.all()
- 前端页面循环显示整个列表。
- 问答排序
{% extends \'base.html\' %} {% block title %} 首页 {% endblock %} {% block head %} <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" integrity="sha384-PsH8R72JQ3SOdhVi3uxftmaW6Vc51MKb0q5P2rRUpPvrszuE4W1povHYgTpBfshb" crossorigin="anonymous"> <link rel="stylesheet" type="text/css"href="../static/css/index.css"> {% endblock %} {% block main %} <div class="card-body"> <div class="card" style="width:60rem; margin:0 auto;opacity:0.9"> <ul class="list-group list-group-flush"> {% for foo in questions %} <li class="list-group-item"> <span class="icon" aria-hidden="true"></span> <br> <h3><a href="{{ url_for(\'detail\',question_id=foo.id) }}">{{ foo.title }}</a></h3> <p>{{ foo.detail }}</p> <span class="badge"style="float:right">{{ foo.creat_time}}</span> <a href="{{ url_for(\'usercenter\',user_id=foo.author.id,tag=1) }}">{{ foo.author.username}}</a> <br> 评论:{{ foo.comments|length }} </li> {% endfor %} </ul> </div> <br> <div class="#" > <div class="img" > <a href="http://www.dji.com/cn"> <img src="https://img14.360buyimg.com/n7/jfs/t5977/17/185587587/108108/247ade15/5925b821N32b8bb9a.jpg"></a> <div class="desc"><a href="http://www.dji.com/cn">大疆“晓”Spark</a></div> </div> <div class="img"> <a href="http://www.dji.com/cn"> <img src="https://img10.360buyimg.com/n7/jfs/t3232/65/6215650755/81612/3a052265/58a27e59N05fb5fc2.jpg" ></a> <div class="desc"><a href="http://www.dji.com/cn">大疆“精灵”Phantom</a></div> </div> <div class="img"> <a href="http://www.dji.com/cn"> <img src="https://img14.360buyimg.com/n7/jfs/t4012/34/459557656/48075/11cf82d3/584fcc5cN056eb123.jpg" ></a> <div class="desc"><a href="http://www.dji.com/cn">大疆“御”Mavic Pro</a></div> </div> <div class="img"> <a href="http://www.dji.com/cn"> <img src="https://img10.360buyimg.com/n7/jfs/t2278/130/1443161174/71119/87b96780/565edd23N23d203bc.jpg"></a> <div class="desc"><a href="http://www.dji.com/cn">大疆“悟”Inspire</a></div> </div> </div> <div class="clearfloat" style="text-align: center"> <img src="https://img14.360buyimg.com/n7/jfs/t5977/17/185587587/108108/247ade15/5925b821N32b8bb9a.jpg"> <img src="https://img10.360buyimg.com/n7/jfs/t3232/65/6215650755/81612/3a052265/58a27e59N05fb5fc2.jpg"> <img src="https://img14.360buyimg.com/n7/jfs/t4012/34/459557656/48075/11cf82d3/584fcc5cN056eb123.jpg"> <img src="https://img10.360buyimg.com/n7/jfs/t2278/130/1443161174/71119/87b96780/565edd23N23d203bc.jpg"> </div> </div> {% endblock %}
首页CSS