
{{blog['content']}}

一、Flask简介
Flask 是一个 Python 实现的 Web 开发微框架。官网:http://flask.pocoo.org/
二、Demo
1、代码结构
5 directories, 8 files
2、主程序blog.py
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'请先登录'
islogin = session.get('islogin')
nav_list = [u'首页',u'经济',u'文化',u'科技',u'娱乐']
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='127.0.0.1')
conn.select_db('blog')
curr = conn.cursor()
sql = 'insert into `user` (`id`,`username`) values (%d,"%s")' % (1,username)
curr.execute(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)
主要有首页、注册、登录、上传页面。
blog.py主要是展示了Flask中常见功能用法:路由,数据库操作,cookie,session,redirect,表单,文件上传,调试,Web服务器的IP和端口,静态文件读取等。
3、首页模板index.html
{{blog['content']}}
4、登录页面login.html
结合blog.py主要展示表单如何提交取值,cookie和session应用。
5、注册页面regist.html
6、上传页面upload.html
7、运行效果
AI-powered app for creating realistic nude photos
Online AI tool for removing clothes from photos.
Undress images for free
AI clothes remover
Swap faces in any video effortlessly with our completely free AI face swap tool!
Easy-to-use and free code editor
Chinese version, very easy to use
Powerful PHP integrated development environment
Visual web development tools
God-level code editing software (SublimeText3)
PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.
PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.
Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.
VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.
VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.
VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
Running Python code in Notepad requires the Python executable and NppExec plug-in to be installed. After installing Python and adding PATH to it, configure the command "python" and the parameter "{CURRENT_DIRECTORY}{FILE_NAME}" in the NppExec plug-in to run Python code in Notepad through the shortcut key "F6".