This article mainly introduces the Portal instance for website development under the PythonWeb framework Flask. This article provides a registration page, logon page, and upload page. For more information, see
1. Introduction to Flask
Flask is a Web development microframework implemented by Python. Official website: http://flask.pocoo.org/
II. Demo
1. code structure
The code is as follows:
.
── Blog. py
── Static
│ ── Css
│ ── Index.css
│ ── Images
│ ── Cat.jpg
│ └ ── Sheying1229.jpg
│ ── Js
└ ── Templates
── Index.html
── Login.html
── Regist.html
── Upload.html
5 directories, 8 files
2. main program blog. py
The code is as follows:
#! /Usr/bin/python
# Coding: utf8
From flask import Flask, render_template, url_for, request, redirect, make_response, session
Import OS, MySQLdb
App = 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 first'
Islogin = session. get ('islogin ')
Nav_list = [u 'homepage ', u 'economical', u 'cultural ', u 'tech', u 'entertaining ']
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
The code is as follows:
Flask DEMO
{% If islogin = '1' %}
Welcome, {username }}!
{% Else %}
{Username }}!
{% Endif %}
{% For nav in nav_list %}
- {Nav }}
{% Endfor %}
{Blog ['title']}
{Blog ['content']}
{% For key, value in blogtag. items () %}
- {Key }}( {value }})
{% Endfor %}
This template mainly shows how to read various types of variables in the Flask template.
Login.html
The code is as follows:
Login
Login
The combination of blog. py mainly shows how the form submits values, cookie and session applications.
5 forbidden registration page regist.html
The code is as follows:
Regist
Regist
Combined with blog. py, it mainly shows database operations.
6、upload page upload.html
The code is as follows:
Upload
Upload
The combination of blog. py mainly shows how to upload files.
7. running effect