from flask import Flask, request, render_template, flash, redirect, url_for from flask.ext.sqlalchemy import SQLAlchemy from flask.ext.wtf import Form from wtforms import StringField, SubmitField from wtforms.validators import Required from flask.ext.bootstrap import Bootstrap
# Create the Flask application app = Flask(__name__)
# Create the Flask-SQLAlchemy object db = SQLAlchemy(app)
@app.route('/', methods=['GET', 'POST']) defindex(): form = InfoForm() if form.validate_on_submit(): info = Info(content=form.content.data) db.session.add(info) db.session.commit() flash(u'添加成功') return redirect(url_for('index')) infos = [{"content": info.content} for info in Info.query.order_by(Info.id.desc())] return render_template('test.html', form=form, infos=infos)
# -*- coding:utf-8 -*- from emoji import app from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
if __name__ == '__main__': app.run(debug=True)
创建数据库emoji, 使用utf8mb4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
➜ ~ mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 149 Server version: 5.5.47-0ubuntu0.14.04.1 (Ubuntu)
Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners.
Type 'help;' or '\h'for help. Type '\c' to clear the current input statement.
➜ ~ python Python 2.7.6 (default, Jun 222015, 17:58:13) [GCC 4.8.2] on linux2 Type "help", "copyright", "credits" or "license"for more information. >>> from emoji import db /home/ming/.virtualenvs/kingmv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning. warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.') >>> db.create_all() >>>
# For each database: ALTERDATABASE database_name CHARACTERSET = utf8mb4 COLLATE utf8mb4_unicode_ci; # For each table: ALTERTABLE table_name CONVERTTOCHARACTERSET utf8mb4 COLLATE utf8mb4_unicode_ci; # For each column: ALTERTABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTERSET utf8mb4 COLLATE utf8mb4_unicode_ci NOTNULL; # (Don’t blindly copy-paste this! The exact statement depends on the column type, maximum length, and other properties. The above line is just an example for a `VARCHAR` column.)