[PYTHON] SNS made with Flask Flask (Blueprint, bcrypt)

TL;DL Udemy attendance record for the following courses

Web application development course with Python + Flask! !! ~ Master Flask from 0 to create SNS ~ https://www.udemy.com/course/flaskpythonweb/

This article describes application management with Flask's BluePrint and password encryption with bcrypt.

What is BluePrint

A function to be used when you want to manage the created application by grouping it by unit such as function.

For example, if you want to manage the modules of the personal information management function (site1) and the data inquiry function (site2) separately, you can manage them separately using BluePrint so that even one application looks like an internal management. Can be managed separately as multiple applications.

Basic structure スクリーンショット 2021-01-03 17.01.22.png

syntax

    1. Create the application you want to manage as a BluePrint object.
format sample URL example created
BluePrint(Site name, __name__, url_prefix='/Character string used as url') mysite1_bp = Blueprint('mysite1', name, url_prefix='/site1') http://www.xxxx/site1/hello
from flask import Blueprint, render_template

'''Create a BlurPrint instance'''
mysite1_bp = Blueprint('mysite1', __name__, url_prefix='/site1')

@mysite1_bp.route('/hello')
def hello():
    return render_template('mysite1/hello.html')
    1. Register the BluePrint object in your application with init.py.
from flask import Flask

def create_app():
    app = Flask(__name__)
    from flaskr.mysite1.views import mysite1_bp
    from flaskr.mysite2.views import mysite2_bp

    app.register_blueprint(mysite1_bp)
    app.register_blueprint(mysite2_bp)
    return app
    1. Get the application information defined in init.py in setup.py and start the application.
from flaskr import create_app
from flask import render_template

app = create_app()  # __init__.Get from app

@app.route('/')
def home():
    return render_template('home.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=True)

Password encryption (bcrypt)

Encrypt passwords using Flask-Bcrypt.

:At the time of inport, "flask"_Note that "bcrypt" and the word feeling are underscore
>>> from flask_bcrypt import Bcrypt

:Creating a bcrypt object
>>> bcrypt = Bcrypt()
>>> testpass = 'password'

:Password hashing
>>> hashed_password = bcrypt.generate_password_hash(password=testpass)
>>> hashed_password
b'$2b$12$3B0I.CHMIEya1OdyI/m44Od7I.TKhRLiOA.EMMWQP3MgUXgr9dkYG'

:Correct / incorrect comparison with hashed password
>>> bcrypt.check_password_hash(hashed_password, 'password')
True
>>> bcrypt.check_password_hash(hashed_password, 'pass')
False

sample

from flask_bcrypt import generate_password_hash, check_password_hash

class User(UserMixin, db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(64), unique=True, index=True)
    username = db.Column(db.String(64), index=True)
    password = db.Column(db.String(128))

    def __init__(self, email, username, password):
        self.email = email
        self.username = username

        #Encrypt the password value passed from the form and store it in a variable.
        self.password = generate_password_hash(password)

    def validate_password(self, password):

        #Compare if the password passed to the function is correct.
        return check_password_hash(self.password, password)

Recommended Posts

SNS made with Flask Flask (Blueprint, bcrypt)
SNS Flask (Ajax) made with Flask
SNS Flask (Model) edition made with Flask
SNS Python basics made with Flask
SNS made with Flask Flask (login process by flask_login)
I made a Mattermost bot with Python (+ Flask)
Container-like # 1 made with C
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (installation)
Container-like # 2 made with C
IP restrictions with Flask
Full-scale server made with Nginx + uWSGI + Flask + Ubuntu (implementation)
Hello world with flask
Programming with Python Flask
Twitter posting client made with Flask with simple login function
How to deploy a web app made with Flask to Heroku
I made a simple book application with python + Flask ~ Introduction ~
(Failure) Deploy a web app made with Flask on heroku
Until API made with Flask + MySQL is converted to Docker
Deploy Flask with ZEIT Now
Pomodoro timer made with Errbot
Hello World with Flask + Hamlish
Unit test flask with pytest
API with Flask + uWSGI + Nginx
I made blackjack with python!
Web application development with Flask
View flask coverage with pytest-cov
Web application with Python + Flask ② ③
I made COVID19_simulator with JupyterLab
I made Word2Vec with Pytorch
File upload with Flask + jQuery
I made blackjack with Python.
Othello made with python (GUI-like)
I made wordcloud with Python.
Web application with Python + Flask ④
I made a Nyanko tweet form with Python, Flask and Heroku