Flask-admin Fast Building Blog Series 1



I want to share a detailed tutorial on Flask + Flask – admin’s rapid blogging, but found that Netease class already has a corresponding free class, so I don’t plan to take notes here bit by bit and share these things. So I mainly focus on the use of flask-admin. If you need Flask, you can refer to Netease’s Flask course. In fact, you want to get a video tutorial in a lecture hall. But you don’t have enough good equipment, and recording video is more like performing, so you can’t adapt to it at once. But I still recorded a set of video effect is not very good, I want to share with you later.

1. Preparing knowledge

Flask-admin is based on the flask framework, so you must first have an overall understanding and learning of flask, at least need to know how to use requests, template responses, database usage and so on.
1) Routing rules for route requests
2) Addition or deletion of Sqlalchemy or other data frameworks
3) the use of jinja2 framework template response

2. Dependency Library, Tool Installation

Python 3.6 + version or more, the specific dependencies are placed in requirements. txt, through PIP3 installation can be done
Using the pycharm tool, you can download it directly from the official website
Pycharm is very simple and easy to use, but requires a registration code, Baidu search directly: pycharm registration code 2017
Then select a registration code to activate; or enter the URL path to activate.

3. Building flask Web

1) Let’s build a framework for flask usage. If you haven’t got an introduction yet, you can use it to enter the door.
Create a new project:file->new project…..
Flask-admin Fast Building Blog Series 1
Input in the console: Python cleanblog.py to start the service
Flask-admin Fast Building Blog Series 1

2) Directory Structure Reform
Firstly, we build the python package of apps, and build a blog Python package in apps. The purpose of setting up this package is to put all the functional modules under apps.
Secondly, the static and template directories are pulled into the apps directory.
In the root directory, add config. py as a configuration file to configure environment variables
Gendb.py is based on flask-sqlalchemy to generate database tables
Flask-admin Fast Building Blog Series 1

3) Integration and transformation

3.1 Integrating Blueprint
The blueprint corresponds to a namespace, and requests routed from the front end are mapped to different processing functions by the address of the blueprint.
In the blog directory, enter the _init_.py file:

from flask import Blueprint
blog = Blueprint('main', __name__)
from . import views

Then register the blueprint in _init_.py of apps:

# Blueprint
from .blog import blog as main_blueprint

In fact, the blueprint registration is app. register_blueprint (main_blueprint, url_prefix=’/XXXX’).
Here XXXX is the path prefix requested to the module.
Make a test example:
View.py in the blog directory has a function:

# coding:utf-8
from . import blog

def getbaseinfo():
    Return "zhe is the test page"

So the request path is www.mxxx.com/XXXX/baseinfo

3.2 Internationalization
Internationally used babelx, an older library, is not ideal. If you customize the background page, you may not need such a library; and now the version of babelx should match flask, otherwise there will be all kinds of abnormalities.
Need to config.py configuration:
Then register in _init_.py of apps:

# Internationalization
from flask_babelex import Babel
babel = Babel(app)

3.3 Local Date Rendering
To use flask_moment to render the date of the template, you need to register with app:

Then the template can be used:

{% block scripts %}
    {{ super() }}
    {moment.include_moment()} /// Reference moment.js file by method in the front end
    {moment.locale ('zh-cn')} // localize moment.js, otherwise rendered time and date are in English
{% endblock %}

{% block page_content %}
    {{ super() }}
    <h3>Time now is {{ moment(current_time).format('LLL') }}</h3><hr>
{% endblock %}

Reference: https://www.cnblogs.com/frank…

4.5 Integration of Sqlalchemy
This is one of our priorities:
Step 1: Registration:
db = SQLAlchemy()
Then register in app:
Step 2: Define the model
Create a model.py file in the apps directory, and you can define the model in it:

#coding: utf-8
import hashlib
from datetime import datetime
from . import db

# User table
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, nullable=False)
    username = db.Column(db.String(64), unique=True, nullable=False)
    role = db.Column(db.String(64), nullable=True)
    password_hash = db.Column(db.String(128))
    head_img = db.Column(db.String(128), unique=False, nullable=True)
    create_time  = db.Column(db.DateTime, nullable=True,default=datetime.now)

    def verity_password(originPassword, password):
        from apps.util import common
        newpassword = common.md5(originPassword)
        return password == newpassword

    def __repr__(self):
        return '<User %r>' % self.username

Step 3: Generate tables
Gendb.py is mainly used to generate database table scripts. You can either go directly to the database to generate scripts.

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from apps import  create_app

# configuration
# SQLALCHEMY_DATABASE_URI = 'mysql://root:[email protected]/flaskAdmin'

app = create_app('config.py')

from apps.models import  db
migrate = Migrate(app,db)
manager = Manager(app)

if __name__ == '__main__':

python  gendb.py db init
python  gendb.py db migrate
python  gendb.py db upgrade

In fact, this file can be passed, and then input directly in the CMD console:
Python gendb.py DB init
Python gendb.py DB migrate
Python gendb.py DB upgrade (synchronized to database)

The first edition is built here, and then we go to the integration of flask-admin.

Github address: https://github.com/minsons/xi…
In addition, Wechat can also open my free video, the effect is not good, in fact, I see enough documents:
Flask-admin Fast Building Blog Series 1