发布功能完成
Posted 008马佳槟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发布功能完成相关的知识,希望对你有一定的参考价值。
from flask import Flask,render_template,request,redirect,url_for,session from flask_sqlalchemy import SQLAlchemy from functools import wraps from datetime import datetime import config,os app=Flask(__name__) app.config.from_object(config) db=SQLAlchemy(app) class User(db.Model): __tablename__=‘user‘ id=db.Column(db.Integer,primary_key=TabError,autoincrement=True) username=db.Column(db.String(20),nullable=False) password=db.Column(db.String(20),nullable=False) nickname = db.Column(db.String(20), nullable=True) class Question(db.Model): __tablename__=‘wenda‘ id=db.Column(db.Integer,primary_key=TabError,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(‘wenda‘)) db.create_all() #是否链接成功 # 数据添加 # user=User(username=‘ma‘,password=‘gg‘) # db.session.add(user) # db.session.commit() # # 数据更新 # # user = User.query.filter(User.username==‘hh‘).first() # user.password=‘gg‘ # db.session.commit() # # # 数据查询 # user = User.query.filter(User.username==‘ma‘).first() # print(user.username,user.password) # # 数据删除 # user = User.query.filter(User.username==‘ma‘).first() # db.session.delete(user) # db.session.commit() # session会话连接 # filter()过滤器 # route制定路径和函数之间的关系 # def定义一个变量 @app.route(‘/‘) def switch(): return render_template(‘switch.html‘) @app.route(‘/login/‘,methods=[‘GET‘,‘POST‘])# 跳转登陆,methods定义它有两种请求方式 def login(): if request.method == ‘GET‘: return render_template(‘login.html‘) else: username = request.form.get(‘user‘) password = request.form.get(‘psw‘) user = User.query.filter(User.username == username).first() # 判断用户名是否存在 if user: if user.password==password: session[‘user‘] = username session.permanent = True return redirect(url_for(‘switch‘)) else: return u‘密码错误‘ else: return u‘用户名不存在‘ @app.context_processor def mycontext(): username=session.get(‘user‘) if username: return {‘username‘:username} else: return{} @app.route(‘/logout‘) def logout(): session.clear() return redirect(url_for(‘switch‘)) @app.route(‘/register/‘,methods=[‘GET‘,‘POST‘])# 跳转注册,methods定义它有两种请求方式 def register(): if request.method == ‘GET‘: return render_template(‘register.html‘) else: username = request.form.get(‘user‘) password = request.form.get(‘psw‘) nickname = request.form.get(‘nickname‘) user = User.query.filter(User.username == username).first() # 判断用户名是否存在 if user: return u‘该用户已存在‘ else: user = User(username=username,password=password,nickname=nickname) 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(‘/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 wenda = Question(title=title,detail=detail,author_id=author_id) db.session.add(wenda) db.session.commit() return redirect(url_for(‘switch‘)) if __name__ == ‘__main__‘: app.run(debug=True)
{% extends ‘switch.html‘ %} {% block wendatitle %}问答界面{% endblock %} {% block wendahead %}{% endblock %} {% block wendabody %} <div class="from-group"> <form action="{{ url_for(‘wenda‘) }}" method="post"> <label for="gb"> 标题 </label> <textarea class="from-control" cols="50" rows="2" id="title" name="title"></textarea> </div> <hr> <hr> <div class="from-group"> <label for="gb1"> 详情</label> <textarea class="from-control" cols="50" rows="10" id="detail" name="detail"></textarea><br> <button type="submit">发送</button> </form> </div> {% endblock %} </html>
以上是关于发布功能完成的主要内容,如果未能解决你的问题,请参考以下文章