码迷,mamicode.com
首页 > 其他好文 > 详细

发布功能完成

时间:2017-11-28 14:19:19      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:block   登录   查询   integer   功能   rman   action   tle   clear   

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>

 

发布功能完成

标签:block   登录   查询   integer   功能   rman   action   tle   clear   

原文地址:http://www.cnblogs.com/154145kj/p/7909414.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!