技术文章和资源

技术文章(时间排序)

热门类别

Python PHP MySQL JDBC Linux

使用 Python Flask 和 MySQL 的配置文件应用程序

pythonserver side programmingprogramming

Flask 是提供库以使用 Python 构建轻量级 Web 应用程序的 Web 框架之一。它是一个微框架,由管理国际 Python 爱好者团体 (POCCO) 的 Armin Ronacher 开发。 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.mysql.com/downloads/workbench/
pip install flask-mysqldb

步骤 − 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

现在我们已经创建了模板文件夹,并在该文件夹中创建了 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

最后,我们必须运行项目服务器并打开 localhost 网页以查看注册表单,无论我们使用注册表单输入什么数据,它们都将在 MYSQL tutorialspointprofile 数据库中更新


相关文章