flask 学习笔记 mvc ,sqlalchemy(insert,update)
生活随笔
收集整理的這篇文章主要介紹了
flask 学习笔记 mvc ,sqlalchemy(insert,update)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
# 模型類 from sqlalchemy import Column, Integer, String from application.database.mysqldb import Baseclass UserModel(Base):__tablename__ = 'user'id = Column(Integer, primary_key=True)name = Column(String)def __init__(self, id=None, name=None):self.id = idself.name = name?
# -*- coding: utf-8 -*- # 數據庫連接類 from sqlalchemy import create_engine from sqlalchemy.orm import scoped_session, sessionmaker from sqlalchemy.ext.declarative import declarative_baseengine = create_engine("mysql+pymysql://root:123456@127.0.0.1:3306/test?charset=utf8",convert_unicode=True,echo=True) db_session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine)) Base = declarative_base() Base.db_session = db_session Base.query = db_session.query_property()def init_db():# 在這里導入所有的可能與定義模型有關的模塊,這樣他們才會合適地# 在 metadata 中注冊。否則,您將不得不在第一次執行 init_db() 時# 先導入他們。Base.metadata.create_all(bind=engine)?
# -*-coding: utf-8 -*- # 控制器類 from flask import render_template, make_response, jsonify, request, flash import json from flask.views import View from application.models.UserModel import UserModelclass User(View):'''用戶類'''methods = ['GET', 'POST']def dispatch_request(self):user = UserModel.query.filter(UserModel.id == 2).first()user_obj = {"id":user.id, "name":user.name}return jsonify(user_obj)class UserLogin(View):methods = ['GET', 'POST']def dispatch_request(self):if request.method == 'POST':username = request.form['username']user = UserModel.query.filter(UserModel.name == username).first()if user is None:_user_save = UserModel(name=username)UserModel.db_session.add(_user_save)UserModel.db_session.commit()flash(u"登陸失敗!")else:UserModel.query.filter(UserModel.id > 2).update({UserModel.name:'tets2'})UserModel.db_session.commit()flash(u"登陸成功!")return render_template('user/login.html')else:return render_template('user/login.html') <!--視圖文件公共模版--> <!doctype html> <html> <head> {% block head %} <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" href="{{ url_for('static', filename='bootstrap-3.3.7-dist/css/bootstrap.min.css') }}"> <title>{% block title %}{% endblock %} - My Webpage</title> {% endblock %}{% with messages = get_flashed_messages() %}{% if messages %}<ul class=flashes>{% for message in messages %}<li>{{ message }}</li>{% endfor %}</ul>{% endif %} {% endwith %} </head> <body><div id="content">{% block content %}{% endblock %}</div>{% block footer %}<div id="footer">© Copyright 2010 by <a href="http://domain.invalid/">you</a>.<script src="{{ url_for('static', filename='plugins/jquery/jQuery-2.1.4.min.js') }}"></script><script src="{{ url_for('static', filename='bootstrap-3.3.7-dist/js/bootstrap.min.js') }}"></script></div>{% endblock %} </body> </html> <!-- 視圖文件(子模版)--> {% extends "common/header.html" %}{% block head %}{{ super() }}<style type="text/css">.important { color: #336699; }</style> {% endblock %}{% block content %}<form class="form-horizontal" method="post"><div class="form-group"><label for="inputEmail3" class="col-sm-2 control-label">用戶名</label><div class="col-sm-10"><input name="username" type="email" class="form-control" id="inputEmail3" placeholder="Email"></div></div><div class="form-group"><label for="inputPassword3" class="col-sm-2 control-label">密碼</label><div class="col-sm-10"><input name="password" type="password" class="form-control" id="inputPassword3" placeholder="Password"></div></div><div class="form-group"><div class="col-sm-offset-2 col-sm-10"><button type="submit" class="btn btn-default">登錄</button></div></div> </form> {% endblock %}{% block footer %}{{ super() }} {% endblock %}轉載于:https://my.oschina.net/phper1234/blog/1498969
總結
以上是生活随笔為你收集整理的flask 学习笔记 mvc ,sqlalchemy(insert,update)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 神经网络基础:七种网络单元,四种层连接方
- 下一篇: 2017面试题4