标签:direct session pos index eth orm mit 映射 on()
def loginFrist(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‘]) @loginFrist 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.fiflter(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‘))
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(‘question‘))
{% extends ‘base.html‘ %} } {% block title %}发布问答{% endblock %} {% block head %} <link rel="stylesheet" href="{{ url_for(‘static‘,filename=‘css/question.css‘) }}" > <script src="{{ url_for(‘static‘,filename=‘JS/question.js‘) }}"></script> {% endblock %} {% block main %} <div class="label0"> <h1>发布问答</h1> <div class="q1"> <label for="question">问题</label> <textarea id="question" cols="50" rows="1"></textarea> </div> <div class="form-group"> <label for="questionDetail">详情</label> <textarea class="form-control" id="questionDetail" cols="50" rows="5"></textarea> </div> <div class="input-area"> <button onclick="fnQuestion">发布问答</button> </div> </div> {% endblock %}
标签:direct session pos index eth orm mit 映射 on()
原文地址:http://www.cnblogs.com/D0204/p/7908153.html