使用 Python Flask 和 MySQL 的个人资料应用程序


Flask 是一个 Web 框架,它提供库来构建轻量级的 Python Web 应用程序。它是一个微型框架,由 Armin Ronacher 开发,他领导着一个国际性的 Python 爱好者团体 (POCCO)。Flask 基于 WSGI 工具包和 Jinja2 模板引擎。

要使用 Python Flask 和 MySQL 创建个人资料应用程序,我们必须按顺序遵循以下步骤。

步骤 1

通过在命令提示符中执行以下命令来安装虚拟环境。

pip install virtualenv

创建虚拟环境后,我们可以创建一个新的虚拟环境到一个文件夹中。

mkdir Flask cd Flask virtualenv venv

创建虚拟环境后,我们必须使用以下命令根据使用的操作系统激活相应的虚拟环境。

在 Linux 操作系统中,要在 shell 中执行以下命令来激活虚拟环境。

venv/bin/activate

在 Windows 操作系统中,要在命令提示符中执行以下命令来激活虚拟环境。

venv\scripts\activate

步骤 2

在此步骤中,我们必须通过执行以下命令将 Flask Web 框架安装到我们的 Python 环境中。

pip install flask

接下来,我们必须使用以下链接和命令安装 MySQL Workbench 和 mysqldb。

https://dev.mysqlserver.cn/downloads/workbench/
pip install flask-mysqldb

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

步骤 3

现在在 MySQL 中创建名为 tutorialspointprofile 的数据库,并将规范添加到个人资料元素中。

create database tutorialspointprofile; SELECT * FROM accounts; create table if not exists accounts( id int(11) not null auto_increment, username varchar(50) not null, password varchar(255) not null, email varchar(100) not null, organisation varchar(100) not null, address varchar(100) not null, city varchar(100) not null, state varchar(100) not null, country varchar(100) not null, postalcode varchar(100) not null, primary key (id) ) engine=InnoDB auto_increment=1 default char set=utf8;

步骤 4

现在,我们必须在 flask 文件夹中创建 app.py 文件,并输入以下代码以与 mysql 数据库和规范一起工作。

from flask import Flask, render_template, request, redirect, url_for, session from flask_mysqldb import MySQL import MySQLdb.cursors import re app = Flask(__name__) app.secret_key = '\xec\x11\xf1\xab\xa9\xf2\x01-F\xd6\xb2wR(\xe4' app.config['MYSQL_HOST'] = 'localhost' app.config['MYSQL_USER'] = 'root' app.config['MYSQL_PASSWORD'] = '$Naavya01?' app.config['MYSQL_DB'] = 'tutorialspointprofile' mysql = MySQL(app) @app.route('/register', methods =['GET', 'POST']) def register(): msg = '' if request.method == 'POST' and 'email' in request.form and 'address' in request.form and 'city' in request.form and 'country' in request.form and 'postalcode' in request.form and 'organisation' in request.form: email = request.form['email'] organisation = request.form['organisation'] address = request.form['address'] city = request.form['city'] state = request.form['state'] country = request.form['country'] postalcode = request.form['postalcode'] cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor) elif not re.match(r'[^@]+@[^@]+\.[^@]+', email): msg = 'Invalid email address !' elif not re.match(r'[A-Za-z0-9]+', username): msg = 'name must contain only characters and numbers !' else: cursor.execute('INSERT INTO accounts VALUES (NULL, % s, % s, % s, % s, % s, % s, % s, % s, % s)', (email, organisation, address, city, state, country, postalcode, )) mysql.connection.commit() msg = 'Successfully registered !' elif request.method == 'POST': msg = 'Please fill out the form !' return render_template('register.html', msg = msg) if __name__ == "__main__": app.run(host ="localhost", port = int("5000"))

步骤 5

现在我们创建了 templates 文件夹,并在该文件夹中创建 HTML 表单,以便用户注册 MySQL 数据库中的详细信息。

<html> <head> <meta charset="UTF-8"> <title> register </title> </head> <body> <div class="registercontent" align="center"> <div class="registertop"> <h1>Register</h1> </div> </br> </br> <div class="registerbottom"> <form action="{{ url_for('register')}}" method="post" autocomplete="off"> <div class="msg">{{ msg }}</div> <input type="email" name="email" placeholder="Enter Your Email ID" class="textbox" id="email" required> <input type="text" name="organisation" placeholder="Enter Your Organisation" class="textbox" id="organisation" required> <input type="text" name="address" placeholder="Enter Your Address" class="textbox" id="address" required> <input type="text" name="city" placeholder="Enter Your City" class="textbox" id="city" required> <input type="text" name="state" placeholder="Enter Your State" class="textbox" id="state" required> <input type="text" name="country" placeholder="Enter Your Country" class="textbox" id="country" required> <input type="text" name="postalcode" placeholder="Enter Your Postal Code" class="textbox" id="postalcode" required> <input type="submit" class="btn" value="Register"> </form> </div> </div> </body> </html>

步骤 6

最后,我们必须运行项目服务器并打开本地主机网页以查看注册表单,以及我们使用注册表单输入的所有数据都将更新到 MYSQL tutorialspointprofile 数据库中。

更新于:2023年10月19日

196 次浏览

开启您的 职业生涯

完成课程后获得认证

开始
广告