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

发布功能完成

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

标签:images   extends   rem   index   一个   word   put   request   ges   

  • 编写要求登录的装饰器

from functools import wraps

def loginFirst(func): #参数是函数

@wraps(func)

      def wrapper(*args, ** kwargs): #定义个函数将其返回

          #要求登录

          return func(*args, ** kwargs)

      return wrapper #返回一个函数

  • 应用装饰器,要求在发布前进行登录,登录后可发布。
@app.route(‘/question/‘,methods=[‘GET‘,‘POST‘])
@loginFirst
def question():

 

  • 建立发布内容的对象关系映射。
class Question(db.Model):
  • 完成发布函数。

保存到数据库。

重定向到首页。

 

from flask import Flask,render_template,request,redirect,url_for,session
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import config
from functools import wraps
from datetime import datetime

app = Flask(__name__)
#app.config[‘SECRET_KEY‘] = ‘1234567‘
app.config.from_object(config)
db = SQLAlchemy(app)

class User(db.Model):
    __tablename__ = user_test
    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))
    tel = db.Column(db.String(20))

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_test.id))
    author = db.relationship(User,backref=db.backref(question))

#db.create_all()

#2.在表中插入一条记录
#user = User(username = ‘mis1‘,password = ‘124‘, nickname=‘wk‘)
#db.session.add(user)
#db.session.commit()


#3.去表中查询用户名对应的密码和接下来用户输入的密码是否相匹配
#user = User.query.filter(User.username == ‘mis1234‘).first()
#print(user)
#print(user.id,user.username)#输出查找到的用户名的id,密码等.

#修改查询到的用户名的密码等
#user = User.query.filter(User.username == ‘mis1234‘).first()
#user.password = ‘123‘
#db.session.commit()

#再次插入一条记录
#user = User(username = ‘wkkkkkk‘,password = ‘123456‘, nickname=‘kk‘)
#db.session.add(user)
#db.session.commit()

#删除一条记录
#user = User.query.filter(User.username == ‘mis1234‘).first()
#db.session.delete(user)
#db.session.commit()




@app.route(/)
def index():
    return render_template(base.html)

@app.route(/login/,methods=[GET,POST])
def login():
    if request.method == GET:
        return render_template(login.html)
    else:
        usern = request.form.get(uname)
        passw = request.form.get(upass)
        user = User.query.filter(User.username==usern).first()
        if user:
            if user.password == passw:
                session[user] = usern;
                return redirect(url_for(index))
            else:
                return password error!
        else:
            return username is not exister!



@app.route(/register/,methods=[GET,POST])
def register():
    if request.method ==GET:
        return  render_template(register.html)
    else:
        usern = request.form.get(uname)
        teln = request.form.get(number)
        passw = request.form.get(upass)
        user = User.query.filter(User.username==usern).first()
        if user:
            return username existed
        else:
            user1= User(username=usern,tel=teln,password=passw)
            db.session.add(user1)
            db.session.commit()
            return redirect(url_for(login))


@app.context_processor
def mycontext():
    usern = session.get(user)
    if usern:
        return{username:usern}
    else:
        return {}

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:
        biaoti = request.form.get(biaoti)
        detail = request.form.get(detail)
        author_id = User.query.filter(User.username==session.get(user)).first().id
        question = Question(title=biaoti,detail=detail,author_id=author_id)

        db.session.add(question)
        db.session.commit()
    return redirect(url_for(index))


if __name__ == __main__:
    app.run(debug=True)
{% extends ‘base.html‘ %}
{% block title %}
    发布问答
{% endblock %}
{% block head %}
    <link rel="stylesheet" type="text/css" href="{{ url_for(‘static‘,filename=‘css/question.css‘)}}">
    <script src="{{ url_for(‘static‘,filename=‘js/login.js‘) }}" type="text/javascript"></script>
{%  endblock %}
{% block main %}
    <!--<h3 style="font-family:华文细黑;color: black">发布问答</h3>-->
    <form  class="all" method="post" action="{{ url_for(‘question‘) }}">
        <h3 >发布问答</h3>
        <div class="form_group"></div>
        <div class="form_group">
            <!--<label for="questionDetial">标题</label>-->
            <!--<textarea class="form-control" rows="1" id="questionDetial"></textarea>-->
            <label for="biaoti" >标题</label>
            <input id="biaoti" type="text" placeholder="请输入标题!" name="biaoti">

        </div>
        <div class="form_group">
            <label for="detail" >详情</label>
            <textarea class="form-control" rows="5" id="detial" name="detail"></textarea>
        </div>


        <div class="checkbox">
            <input type="checkbox"> check me out <br>
            <button type="submit" class="btn bun-default">发布</button>
        </div>
    </form>

{%  endblock %}

技术分享图片技术分享图片

发布功能完成

标签:images   extends   rem   index   一个   word   put   request   ges   

原文地址:http://www.cnblogs.com/wk15/p/7909049.html

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