Flask knowledge flask knowledge


app.config [ 'JSON_AS_ASCII'] = False # Chinese Conversion
@ app.route ( '/ user / < user>') dynamic routing, by the function parameter passing
jsonify () is converted into page can see understand json string
redirct ( '' ) Jump routing address
redirct (url_for (function name)) jump function name
abort (404) thrown
'{}'. format (function) dynamic format format parameter passing

max_age = 3600 seconds expiration time set
ck = make_response () to generate a complex page returned to the browser memory
ck.set_cookie () is stored in a cookie
user = request.cookies.get ( 'user_name') request the data in the cookies
app. secret_key = '1811' key
session [ 'user_id'] = id sessio stored in the
user = session.get ( 'user_id') obtaining the session data
*
rs = jsonify (MSG) to identify utf8 showing Chinese
rs .headers [ 'the Type-conten'] = 'file application / JSON; charset = UTF-. 8'
*
**
from flask_script Import Manager
Manage = Manager (App)
manage.run () py ran window file black
python file name. py runserver * - host ip address --port port number *
**

render_template (html.html) template name specified
app = Flask (__ name __, template_folder = '.. / templates') py file path location specified templates must same level
| decorator
{% filter ()%} can modify all of the intermediate form {% Endfilter%}
{% IF%} _ {% elif%} _ {% the else%} _ {% endif%} is determined format, and the start and end
{% for%} _ {% endfor%} for loop
{{ loop.index}} serial number, starting from 1. 0 plus from zero

{% Extends 'parent template name .html'%} are designated inherit the parent template
{% block (name)%} {% endblock%} template inheritance part
{% include 'parent template name .html'%} overall page is to contain take over
{% for i in get_flashed_messages () %} template flash
flash () py flashed
<script> alert ( '{{variable name}}') </ script> output frame shells

from flask_sqlalchemy import SQLAlchemy guide packet connection database
app.config [ 'SQLALCHEMY_DATABASE_URI'] = ' mysql + pymysql: // root: @ 127.0.0.1: 3306 / book'
database connection XXX: // username: password: @ database IP: ports / library name
app.config [ 'SQLALCHEMY_TRACK_MODIFICATIONS'] = True
dynamic tracking modify settings, if not set will remind warning
app.config [ 'SQLALCHEMY_ECHO'] = True to display original query SQL statement
db = SQLAlchemy (app) db with to operate the database, and the custom model class associated
class author (db.Model): db.Model call db
__tablename__ = 'author' to modify the table name
id = db.Column (db.Integer) construction field
db.create_all () to create database table
db.drop_all () delete the database table
books = db.relationship ( 'Books category name', backref = 'table name from author', lazy = 'Dynamic')
relationship relationship backref = '' reverse lookup lazy = ' dynamic dynamically loaded
def __str __ (self):return str (self.id) + '|' + self.name into a string
book_list = db.session.execute ( 'select * from books') supports native loop through sql statement
a1 = Author.query.all () See table query.all all data () Query corresponds to loop through select
a2 = Author. query.filter (author.name == 'John Doe') corresponds to the condition where a query statement to loop through filter
.first () added at the end, return to the first query
from sqlalchemy import and_, not_, or_ non conductive package or
a4 = Author.query.filter (and_ (author.name == 'John Doe', author.id == 1)). first with and_
A5 = Author.query.filter (or_ (author.name == 'John Doe' , author.id == 1)) or or_ cycle through
a6 = Author.query.filter (not_ (author.name == ' John Doe')) non Not_ cycle through
a7 = Author.query.filter (author.name = = 'Joe Smith') .first () for i in a7.books: look forward to many inquiries, according to the authors find books
b1 = Books.query.filter (Books.id == 1) .first () print ( b1.author) reverse lookup, according to authors of books looking
statements there is a change to data in the database should be carried out db.session commit()
a8 = Author (name = 'Wangwu') db.session.add (a8) was added a data
a9 = Author () a9.name = 'Zhao six 1' db.session.add_all ([a9, a10 , a11]) adding a plurality of data table
a12 = Author (name = 'can Liu') b1 = Books (name = ' Country love 1') a12.books = [b1, b2, b3] db.session.add (a12) a to add more data while adding authors and books
a13 = Author.query.filter (author.name == 'Liu Neng') .first () a13.name = 'Zhao Si' modify data
a14 = Author.query.filter (author .name == 'Zhao six 1'). first () db.session.delete (a14) delete data
a15 = Author.query.filter (author.name == 'Zhao Si') .first () a15.books. delete () delete many

{% For message in get_flashed_messages%} front flash content displayed
post submission values request.form.get ( 'User')
GET submission values request.args.get ( 'User')
from the Migrate flask_migrate Import, database migration MigrateCommand leader packet
from flask_script import Manager window turned black packet
migrate = migrate (app, db) 1 to databases to update the database table black window structure
manager.add_command ( 'db', MigrateCommand) 2 databases to the black window to update the database table structure
database migration operation sequence
1.python db init file folder build Migrations
2.python file db migrate -m "version of the name (comment)"
3.python file db upgrade and then observe the table structure
of the main table: cascade = 'all, delete -orphan ', passive_deletes = True
child table: ondelete =' CASCADE 'association deleted

<script src = "../../ static / js / jquery-3.3.1.min.js"> </ script> introducing the jQuery
the console.log ($ ( '. One'). Val ()) selected ID is Val () Gets the value argument
console.log ($ ( '# cla' ). val ()) class selector
$ ( '# one'). val ( '123') controls the assignment
$ ( '# btn'). ({. $ ( '# one ') val ( ' content')} function ()) click button click event
the parseInt () is converted to type int
def to_dict (self): converted into a dictionary
window.location.href = '/ user / login '; jump address reception method
colspan = "6" merged cell align = "right" right display
<style> ul {list-style : none; black spot elimination}
UL {Li the display: Block; variable block margin-right: 10px; margin float: left; left}

Blueprint introduced from modules.admin import admin_blue package
app.register_blueprint (admin_blue, url_prefix = '/ admin') register blueprint designated blueprint
admin_blue = Blueprint ( 'admin', __ name__) disposed blueprint

import logging logs arranged leader packet
from logging.handlers import RotatingFileHandler

from werkzeug.security import generate_password_hash, check_password_hash password encryption (generation) (Comparative)

Photo form, needs to be added in the form: the enctype = "multipart / form-Data" Method = "POST"
from utils.captcha.captcha captcha generating codes Import Image
type = 'file' upload
.lower () Size ignoring write

window. js Jump ######

img = request.files.get ( 'avatar') files Get File


app.config [ 'JSON_AS_ASCII'] = False # Chinese Conversion
@ app.route ( '/ user / < user>') dynamic routing, by the function parameter passing
jsonify () is converted into page can see understand json string
redirct ( '' ) Jump routing address
redirct (url_for (function name)) jump function name
abort (404) thrown
'{}'. format (function) dynamic format format parameter passing

max_age = 3600 seconds expiration time set
ck = make_response () to generate a complex page returned to the browser memory
ck.set_cookie () is stored in a cookie
user = request.cookies.get ( 'user_name') request the data in the cookies
app. secret_key = '1811' key
session [ 'user_id'] = id sessio stored in the
user = session.get ( 'user_id') obtaining the session data
*
rs = jsonify (MSG) to identify utf8 showing Chinese
rs .headers [ 'the Type-conten'] = 'file application / JSON; charset = UTF-. 8'
*
**
from flask_script Import Manager
Manage = Manager (App)
manage.run () py ran window file black
python file name. py runserver * - host ip address --port port number *
**

render_template (html.html) template name specified
app = Flask (__ name __, template_folder = '.. / templates') py file path location specified templates must same level
| decorator
{% filter ()%} can modify all of the intermediate form {% Endfilter%}
{% IF%} _ {% elif%} _ {% the else%} _ {% endif%} is determined format, and the start and end
{% for%} _ {% endfor%} for loop
{{ loop.index}} serial number, starting from 1. 0 plus from zero

{% Extends 'parent template name .html'%} are designated inherit the parent template
{% block (name)%} {% endblock%} template inheritance part
{% include 'parent template name .html'%} overall page is to contain take over
{% for i in get_flashed_messages () %} template flash
flash () py flashed
<script> alert ( '{{variable name}}') </ script> output frame shells

from flask_sqlalchemy import SQLAlchemy guide packet connection database
app.config [ 'SQLALCHEMY_DATABASE_URI'] = ' mysql + pymysql: // root: @ 127.0.0.1: 3306 / book'
database connection XXX: // username: password: @ database IP: ports / library name
app.config [ 'SQLALCHEMY_TRACK_MODIFICATIONS'] = True
dynamic tracking modify settings, if not set will remind warning
app.config [ 'SQLALCHEMY_ECHO'] = True to display original query SQL statement
db = SQLAlchemy (app) db with to operate the database, and the custom model class associated
class author (db.Model): db.Model call db
__tablename__ = 'author' to modify the table name
id = db.Column (db.Integer) construction field
db.create_all () to create database table
db.drop_all () delete the database table
books = db.relationship ( 'Books category name', backref = 'table name from author', lazy = 'Dynamic')
relationship relationship backref = '' reverse lookup lazy = ' dynamic dynamically loaded
def __str __ (self):return str (self.id) + '|' + self.name into a string
book_list = db.session.execute ( 'select * from books') supports native loop through sql statement
a1 = Author.query.all () See table query.all all data () Query corresponds to loop through select
a2 = Author. query.filter (author.name == 'John Doe') corresponds to the condition where a query statement to loop through filter
.first () added at the end, return to the first query
from sqlalchemy import and_, not_, or_ non conductive package or
a4 = Author.query.filter (and_ (author.name == 'John Doe', author.id == 1)). first with and_
A5 = Author.query.filter (or_ (author.name == 'John Doe' , author.id == 1)) or or_ cycle through
a6 = Author.query.filter (not_ (author.name == ' John Doe')) non Not_ cycle through
a7 = Author.query.filter (author.name = = 'Joe Smith') .first () for i in a7.books: look forward to many inquiries, according to the authors find books
b1 = Books.query.filter (Books.id == 1) .first () print ( b1.author) reverse lookup, according to authors of books looking
statements there is a change to data in the database should be carried out db.session commit()
a8 = Author (name = 'Wangwu') db.session.add (a8) was added a data
a9 = Author () a9.name = 'Zhao six 1' db.session.add_all ([a9, a10 , a11]) adding a plurality of data table
a12 = Author (name = 'can Liu') b1 = Books (name = ' Country love 1') a12.books = [b1, b2, b3] db.session.add (a12) a to add more data while adding authors and books
a13 = Author.query.filter (author.name == 'Liu Neng') .first () a13.name = 'Zhao Si' modify data
a14 = Author.query.filter (author .name == 'Zhao six 1'). first () db.session.delete (a14) delete data
a15 = Author.query.filter (author.name == 'Zhao Si') .first () a15.books. delete () delete many

{% For message in get_flashed_messages%} front flash content displayed
post submission values request.form.get ( 'User')
GET submission values request.args.get ( 'User')
from the Migrate flask_migrate Import, database migration MigrateCommand leader packet
from flask_script import Manager window turned black packet
migrate = migrate (app, db) 1 to databases to update the database table black window structure
manager.add_command ( 'db', MigrateCommand) 2 databases to the black window to update the database table structure
database migration operation sequence
1.python db init file folder build Migrations
2.python file db migrate -m "version of the name (comment)"
3.python file db upgrade and then observe the table structure
of the main table: cascade = 'all, delete -orphan ', passive_deletes = True
child table: ondelete =' CASCADE 'association deleted

<script src = "../../ static / js / jquery-3.3.1.min.js"> </ script> introducing the jQuery
the console.log ($ ( '. One'). Val ()) selected ID is Val () Gets the value argument
console.log ($ ( '# cla' ). val ()) class selector
$ ( '# one'). val ( '123') controls the assignment
$ ( '# btn'). ({. $ ( '# one ') val ( ' content')} function ()) click button click event
the parseInt () is converted to type int
def to_dict (self): converted into a dictionary
window.location.href = '/ user / login '; jump address reception method
colspan = "6" merged cell align = "right" right display
<style> ul {list-style : none; black spot elimination}
UL {Li the display: Block; variable block margin-right: 10px; margin float: left; left}

Blueprint introduced from modules.admin import admin_blue package
app.register_blueprint (admin_blue, url_prefix = '/ admin') register blueprint designated blueprint
admin_blue = Blueprint ( 'admin', __ name__) disposed blueprint

import logging logs arranged leader packet
from logging.handlers import RotatingFileHandler

from werkzeug.security import generate_password_hash, check_password_hash password encryption (generation) (Comparative)

Photo form, needs to be added in the form: the enctype = "multipart / form-Data" Method = "POST"
from utils.captcha.captcha captcha generating codes Import Image
type = 'file' upload
.lower () Size ignoring write

window. js Jump ######

img = request.files.get ( 'avatar') files Get File

Guess you like

Origin www.cnblogs.com/wyf2019/p/10972728.html