Python study note 13: Flask demo, pythonflask
1. Introduction to Flask
Flask is a Web development microframework implemented by Python. Official Website: http://flask.pocoo.org/
Ii. Demo1. code structure
.├── blog.py├── static│ ├── css│ │ └── index.css│ ├── images│ │ ├── cat.jpg│ │ └── sheying1229.jpg│ └── js└── templates ├── index.html ├── login.html ├── regist.html └── upload.html5 directories, 8 files
2. Main Program blog. py
#! /Usr/bin/python # coding: utf8from flask import Flask, render_template, url_for, request, redirect, make_response, sessionimport OS, MySQLdbapp = Flask (_ name _) app. secret_key = 'afjlsjfowflajflkajfkjfkaljf' user _ list = ['jim', 'max ', 'py'] imagepath = OS. path. join (OS. getcwd (), "static/images") @ app. route ('/') def index (): username = request. cookies. get ('username') if not username: username = U' log on to 'islogin = ses Sion. get ('islogin') nav_list = [u 'homepage ', u 'economical', u 'cultural ', u 'tech ', u'enter'] blog = {'title': 'Welcome to my blog', 'content': 'Hello, welcome to my blog. '} blogtag = {'javascript': 10, "python": 20, "shell": 5} img = url_for ('static ', filename = "images/cat.jpg ") return render_template('index.html ', nav_list = nav_list, username = username, blog = blog, blogtag = blogtag, img = img, islogin = islogin) @ app. route ('/reg', methods = ['Get', 'post']) def regist (): if request. method = 'post': username = request. form ['username'] conn = MySQLdb. connect (user = 'root', passwd = 'admin', host = '2017. 0.0.1 ') conn. select_db ('blog ') curr = conn. cursor () SQL = 'insert into 'user' ('id', 'username') values (% d, "% s") '% (1, username) curr.exe cute (SQL) conn. commit () curr. close () conn. close () return "user % s regist OK! "% Request. form ['username'] else: # request. args ['username'] return render_template('regist.html ') @ app. route ('/upload', methods = ['get', 'post']) def upload (): if request. method = 'post': username = request. form ['username'] file = request. files ['img '] filename = file. filename file. save (OS. path. join (imagepath, filename) return " "% filename else: return render_template('upload.html ') @ app. route ('/login/', methods = ['get', 'post']) def login (): if request. method = 'post': username = request. form. get ('username') if username in user_list: response = make_response (redirect ('/') response. set_cookie ('username', value = username, max_age = 300) session ['islogin'] = '1' return response else: session ['islogin'] = '0' return redirect ('/login/') else: return render_template('login.html ') if _ name _ = '_ main _': app. run (debug = True, host = '0. 0.0.0 ', port = 5000)
It mainly includes the homepage, registration, logon, and upload pages.
Blog. py shows common functions and usage of Flask: routing, database operations, cookie, session, redirect, form, file upload, debugging, IP address and port of the Web server, static File Reading.
3rd, the first page template index.html
<!DOCTYPE html>
This template mainly shows how to read various types of variables in the Flask template.
Login.html
<!DOCTYPE html>
The combination of blog. py mainly shows how the form submits values, cookie and session applications.
5 forbidden registration page regist.html
<!DOCTYPE html>
Combined with blog. py, it mainly shows database operations.
6、upload page upload.html
<!DOCTYPE html>
The combination of blog. py mainly shows how to upload files.
7. Running Effect
Iii. References
1. Flask Quick Start