Simple blog project creation based on Flask (global variable setting and import module), flask global variable

Source: Internet
Author: User

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 !!!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.