Simple blog project creation based on Flask (global variable setting and import module), flask global variable
This is a course I learned in the Lab Building (https://www.shiyanlou.com/courses/29/labs/264/document)
But what is different from the original tutorial is that I use a mysql database, so there are many differences in the configuration file. Here I want to share with those who do not want to install SQLite, people with their own ideas can serve as a reference
The following is the configuration file:
Base = declarative.declarative_base()
# The Object Relational Mapping (ORM) User class corresponds to the table entries in the database. By the way, never miss the Base)
Class User (Base): _ tablename _ = 'entries' title = Column (String (20), primary_key = True) text = Column (String (60 )) def _ init _ (self, title, text): self. title = title self. text = text "" If you write
Create_engine ('mysql + your database DRIVER: // your Username: Your password @ localhost: 3306/flaskr ', encoding = 'utf-8 '), you will be pleasantly surprised to find Chinese garbled characters
This is because you have not correctly specified UTF-8 encoding.
Friendly reminder: Use the following encoding and write it as utf8. Otherwise, an error will be reported.
In addition, my database driver chooses pymysql.
"""
Engine = create_engine ('mysql + your database DRIVER: // your Username: Your password @ localhost: 3306/flaskr? Charset = utf8 ',)
DBSession = sessionmaker (bind = engine)
USERNAME = 'admin'
PASSWORD = 'default'
# SECRET_KEY cannot be missed. Otherwise, the program runs incorrectly. Please use Baidu for the specific principle. The bloggers are powerless.
SECRET_KEY = 'development key'
App = Flask (_ name __)
App. config. from_object (_ name __)
By the way, paste the imported module:
from sqlalchemy import *from flask import Flask, request, session, g, redirect, url_for, abort, render_template, flashfrom sqlalchemy.ext import declarativefrom sqlalchemy.orm import *
In the end, I want to tell the novice python a very important thing. pycharm is a very good and powerful IDE. Let alone anything else. A good IDE can let us DEBUG without authorization! DEBUG! DEBUG !!!