使用 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.mysql.com.tw/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

現在我們建立了 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 次瀏覽

開啟您的 職業生涯

完成課程後獲得認證

開始
廣告
© . All rights reserved.