{"id":1177239,"date":"2025-01-15T17:55:44","date_gmt":"2025-01-15T09:55:44","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1177239.html"},"modified":"2025-01-15T17:55:47","modified_gmt":"2025-01-15T09:55:47","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%bc%80%e5%8f%91%e9%a5%ad%e5%a0%82%e5%85%85%e5%80%bc","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1177239.html","title":{"rendered":"\u5982\u4f55\u7528python\u5f00\u53d1\u996d\u5802\u5145\u503c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25112208\/b48301c2-625b-4e42-adbd-7f4db1915d95.webp\" alt=\"\u5982\u4f55\u7528python\u5f00\u53d1\u996d\u5802\u5145\u503c\" \/><\/p>\n<p><p> <strong>\u8981\u4f7f\u7528Python\u5f00\u53d1\u996d\u5802\u5145\u503c\u7cfb\u7edf\uff0c\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u8fd9\u6837\u7684Web\u6846\u67b6\u3001\u4f7f\u7528SQLite\u6216MySQL\u8fd9\u6837\u7684\u6570\u636e\u5e93\u3001\u4f7f\u7528HTML\/CSS\/JavaScript\u8fdb\u884c\u524d\u7aef\u5f00\u53d1\u3002<\/strong> \u8fd9\u91cc\u6211\u4eec\u9009\u62e9Flask\u548cSQLite\u6765\u8fdb\u884c\u5f00\u53d1\uff0c\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5b89\u88c5\u548c\u914d\u7f6e\u73af\u5883<\/strong>\uff1a\u9996\u5148\u9700\u8981\u5b89\u88c5Python\u548c\u76f8\u5173\u7684\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u6765\u5b89\u88c5Flask\u548cSQLite\u3002<\/li>\n<li><strong>\u8bbe\u8ba1\u6570\u636e\u5e93<\/strong>\uff1a\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u6570\u636e\u5e93\u6765\u5b58\u50a8\u7528\u6237\u4fe1\u606f\u548c\u5145\u503c\u8bb0\u5f55\u3002<\/li>\n<li><strong>\u5f00\u53d1\u540e\u7aef<\/strong>\uff1a\u4f7f\u7528Flask\u6765\u5904\u7406\u7528\u6237\u8bf7\u6c42\uff0c\u5e76\u4e0e\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\u3002<\/li>\n<li><strong>\u5f00\u53d1\u524d\u7aef<\/strong>\uff1a\u4f7f\u7528HTML\/CSS\/JavaScript\u6765\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u3002<\/li>\n<li><strong>\u6d4b\u8bd5\u548c\u90e8\u7f72<\/strong>\uff1a\u5bf9\u7cfb\u7edf\u8fdb\u884c\u6d4b\u8bd5\uff0c\u5e76\u5c06\u5176\u90e8\u7f72\u5230\u670d\u52a1\u5668\u4e0a\u3002<\/li>\n<\/ol>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u6bcf\u4e2a\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u5b89\u88c5\u548c\u914d\u7f6e\u73af\u5883<\/strong><\/h2>\n<p><h2>1. \u5b89\u88c5Python<\/h2>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5Python\u3002\u5982\u679c\u4f60\u8fd8\u6ca1\u6709\u5b89\u88c5Python\uff0c\u53ef\u4ee5\u4ece<a href=\"https:\/\/www.python.org\/\">Python\u5b98\u65b9\u7f51\u7ad9<\/a>\u4e0b\u8f7d\u5e76\u5b89\u88c5\u6700\u65b0\u7248\u672c\u7684Python\u3002<\/p>\n<\/p>\n<p><h2>2. \u521b\u5efa\u865a\u62df\u73af\u5883<\/h2>\n<\/p>\n<p><p>\u5efa\u8bae\u4f7f\u7528\u865a\u62df\u73af\u5883\u6765\u7ba1\u7406\u4f60\u7684Python\u9879\u76ee\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u548c\u6fc0\u6d3b\u865a\u62df\u73af\u5883\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python -m venv venv<\/p>\n<p>source venv\/bin\/activate  # \u5728Windows\u4e0a\u4f7f\u7528 venv\\Scripts\\activate<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u5b89\u88c5Flask\u548cSQLite<\/h2>\n<\/p>\n<p><p>\u4f7f\u7528pip\u6765\u5b89\u88c5Flask\u548cSQLite\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install Flask<\/p>\n<p>pip install sqlite3<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u8bbe\u8ba1\u6570\u636e\u5e93<\/strong><\/h2>\n<p><h2>1. \u6570\u636e\u5e93\u7ed3\u6784<\/h2>\n<\/p>\n<p><p>\u6211\u4eec\u9700\u8981\u8bbe\u8ba1\u4e00\u4e2a\u6570\u636e\u5e93\u6765\u5b58\u50a8\u7528\u6237\u4fe1\u606f\u548c\u5145\u503c\u8bb0\u5f55\u3002\u53ef\u4ee5\u521b\u5efa\u4e24\u4e2a\u8868\uff1a\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u7528\u6237\u4fe1\u606f\uff08users\uff09\uff0c\u53e6\u4e00\u4e2a\u7528\u4e8e\u5b58\u50a8\u5145\u503c\u8bb0\u5f55\uff08transactions\uff09\u3002<\/p>\n<\/p>\n<p><h3>\u7528\u6237\u8868\uff08users\uff09<\/h3>\n<\/p>\n<table>\n<thead>\n<tr>\n<p><th>\u5b57\u6bb5\u540d<\/th>\n<\/p>\n<p><th>\u6570\u636e\u7c7b\u578b<\/th>\n<\/p>\n<p><th>\u8bf4\u660e<\/th>\n<\/p>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<p><td>id<\/td>\n<\/p>\n<p><td>INTEGER<\/td>\n<\/p>\n<p><td>\u4e3b\u952e\uff0c\u81ea\u589e<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>username<\/td>\n<\/p>\n<p><td>TEXT<\/td>\n<\/p>\n<p><td>\u7528\u6237\u540d<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>password<\/td>\n<\/p>\n<p><td>TEXT<\/td>\n<\/p>\n<p><td>\u5bc6\u7801\uff08\u52a0\u5bc6\u5b58\u50a8\uff09<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>balance<\/td>\n<\/p>\n<p><td>REAL<\/td>\n<\/p>\n<p><td>\u4f59\u989d<\/td>\n<\/p>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><h3>\u5145\u503c\u8bb0\u5f55\u8868\uff08transactions\uff09<\/h3>\n<\/p>\n<table>\n<thead>\n<tr>\n<p><th>\u5b57\u6bb5\u540d<\/th>\n<\/p>\n<p><th>\u6570\u636e\u7c7b\u578b<\/th>\n<\/p>\n<p><th>\u8bf4\u660e<\/th>\n<\/p>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<p><td>id<\/td>\n<\/p>\n<p><td>INTEGER<\/td>\n<\/p>\n<p><td>\u4e3b\u952e\uff0c\u81ea\u589e<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>user_id<\/td>\n<\/p>\n<p><td>INTEGER<\/td>\n<\/p>\n<p><td>\u7528\u6237ID\uff08\u5916\u952e\uff09<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>amount<\/td>\n<\/p>\n<p><td>REAL<\/td>\n<\/p>\n<p><td>\u5145\u503c\u91d1\u989d<\/td>\n<\/p>\n<\/tr>\n<tr>\n<p><td>timestamp<\/td>\n<\/p>\n<p><td>DATETIME<\/td>\n<\/p>\n<p><td>\u5145\u503c\u65f6\u95f4<\/td>\n<\/p>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><h2>2. \u521b\u5efa\u6570\u636e\u5e93<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u6765\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sqlite3<\/p>\n<p>def init_db():<\/p>\n<p>    conn = sqlite3.connect(&#39;canteen.db&#39;)<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;&#39;&#39;<\/p>\n<p>    CREATE TABLE IF NOT EXISTS users (<\/p>\n<p>        id INTEGER PRIMARY KEY AUTOINCREMENT,<\/p>\n<p>        username TEXT NOT NULL,<\/p>\n<p>        password TEXT NOT NULL,<\/p>\n<p>        balance REAL DEFAULT 0<\/p>\n<p>    )<\/p>\n<p>    &#39;&#39;&#39;)<\/p>\n<p>    c.execute(&#39;&#39;&#39;<\/p>\n<p>    CREATE TABLE IF NOT EXISTS transactions (<\/p>\n<p>        id INTEGER PRIMARY KEY AUTOINCREMENT,<\/p>\n<p>        user_id INTEGER NOT NULL,<\/p>\n<p>        amount REAL NOT NULL,<\/p>\n<p>        timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,<\/p>\n<p>        FOREIGN KEY (user_id) REFERENCES users (id)<\/p>\n<p>    )<\/p>\n<p>    &#39;&#39;&#39;)<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>if __name__ == &#39;__m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n__&#39;:<\/p>\n<p>    init_db()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e09\u3001\u5f00\u53d1\u540e\u7aef<\/strong><\/h2>\n<p><h2>1. \u521b\u5efaFlask\u5e94\u7528<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2aFlask\u5e94\u7528\u6765\u5904\u7406\u7528\u6237\u8bf7\u6c42\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, jsonify<\/p>\n<p>import sqlite3<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>def get_db_connection():<\/p>\n<p>    conn = sqlite3.connect(&#39;canteen.db&#39;)<\/p>\n<p>    conn.row_factory = sqlite3.Row<\/p>\n<p>    return conn<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    return &#39;Welcome to the Canteen Recharge System&#39;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u7528\u6237\u6ce8\u518c\u548c\u767b\u5f55<\/h2>\n<\/p>\n<p><h3>\u7528\u6237\u6ce8\u518c<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u6ce8\u518c\u529f\u80fd\u9700\u8981\u63a5\u6536\u7528\u6237\u540d\u548c\u5bc6\u7801\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u5728\u6570\u636e\u5e93\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from werkzeug.security import generate_password_hash, check_password_hash<\/p>\n<p>@app.route(&#39;\/register&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def register():<\/p>\n<p>    username = request.form[&#39;username&#39;]<\/p>\n<p>    password = request.form[&#39;password&#39;]<\/p>\n<p>    conn = get_db_connection()<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;SELECT * FROM users WHERE username = ?&#39;, (username,))<\/p>\n<p>    if c.fetchone() is not None:<\/p>\n<p>        return jsonify({&#39;error&#39;: &#39;User already exists&#39;}), 400<\/p>\n<p>    hashed_password = generate_password_hash(password)<\/p>\n<p>    c.execute(&#39;INSERT INTO users (username, password) VALUES (?, ?)&#39;, (username, hashed_password))<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>    return jsonify({&#39;message&#39;: &#39;User registered successfully&#39;}), 201<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u7528\u6237\u767b\u5f55<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u767b\u5f55\u529f\u80fd\u9700\u8981\u9a8c\u8bc1\u7528\u6237\u540d\u548c\u5bc6\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/login&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def login():<\/p>\n<p>    username = request.form[&#39;username&#39;]<\/p>\n<p>    password = request.form[&#39;password&#39;]<\/p>\n<p>    conn = get_db_connection()<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;SELECT * FROM users WHERE username = ?&#39;, (username,))<\/p>\n<p>    user = c.fetchone()<\/p>\n<p>    if user is None or not check_password_hash(user[&#39;password&#39;], password):<\/p>\n<p>        return jsonify({&#39;error&#39;: &#39;Invalid username or password&#39;}), 401<\/p>\n<p>    return jsonify({&#39;message&#39;: &#39;User logged in successfully&#39;, &#39;user_id&#39;: user[&#39;id&#39;]}), 200<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u5145\u503c\u529f\u80fd<\/h2>\n<\/p>\n<p><p>\u5145\u503c\u529f\u80fd\u9700\u8981\u63a5\u6536\u7528\u6237ID\u548c\u5145\u503c\u91d1\u989d\uff0c\u5e76\u66f4\u65b0\u7528\u6237\u4f59\u989d\u548c\u5145\u503c\u8bb0\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/recharge&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def recharge():<\/p>\n<p>    user_id = request.form[&#39;user_id&#39;]<\/p>\n<p>    amount = float(request.form[&#39;amount&#39;])<\/p>\n<p>    conn = get_db_connection()<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;SELECT * FROM users WHERE id = ?&#39;, (user_id,))<\/p>\n<p>    user = c.fetchone()<\/p>\n<p>    if user is None:<\/p>\n<p>        return jsonify({&#39;error&#39;: &#39;User not found&#39;}), 404<\/p>\n<p>    new_balance = user[&#39;balance&#39;] + amount<\/p>\n<p>    c.execute(&#39;UPDATE users SET balance = ? WHERE id = ?&#39;, (new_balance, user_id))<\/p>\n<p>    c.execute(&#39;INSERT INTO transactions (user_id, amount) VALUES (?, ?)&#39;, (user_id, amount))<\/p>\n<p>    conn.commit()<\/p>\n<p>    conn.close()<\/p>\n<p>    return jsonify({&#39;message&#39;: &#39;Recharge successful&#39;, &#39;new_balance&#39;: new_balance}), 200<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>4. \u67e5\u8be2\u4f59\u989d\u548c\u5145\u503c\u8bb0\u5f55<\/h2>\n<\/p>\n<p><h3>\u67e5\u8be2\u4f59\u989d<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u53ef\u4ee5\u67e5\u8be2\u81ea\u5df1\u7684\u4f59\u989d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/balance&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def balance():<\/p>\n<p>    user_id = request.args.get(&#39;user_id&#39;)<\/p>\n<p>    conn = get_db_connection()<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;SELECT balance FROM users WHERE id = ?&#39;, (user_id,))<\/p>\n<p>    user = c.fetchone()<\/p>\n<p>    if user is None:<\/p>\n<p>        return jsonify({&#39;error&#39;: &#39;User not found&#39;}), 404<\/p>\n<p>    return jsonify({&#39;balance&#39;: user[&#39;balance&#39;]}), 200<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u67e5\u8be2\u5145\u503c\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u53ef\u4ee5\u67e5\u8be2\u81ea\u5df1\u7684\u5145\u503c\u8bb0\u5f55\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/transactions&#39;, methods=[&#39;GET&#39;])<\/p>\n<p>def transactions():<\/p>\n<p>    user_id = request.args.get(&#39;user_id&#39;)<\/p>\n<p>    conn = get_db_connection()<\/p>\n<p>    c = conn.cursor()<\/p>\n<p>    c.execute(&#39;SELECT * FROM transactions WHERE user_id = ? ORDER BY timestamp DESC&#39;, (user_id,))<\/p>\n<p>    transactions = c.fetchall()<\/p>\n<p>    return jsonify([dict(tx) for tx in transactions]), 200<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u5f00\u53d1\u524d\u7aef<\/strong><\/h2>\n<p><h2>1. \u8bbe\u8ba1\u767b\u5f55\u548c\u6ce8\u518c\u9875\u9762<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528HTML\/CSS\u6765\u8bbe\u8ba1\u7528\u6237\u767b\u5f55\u548c\u6ce8\u518c\u9875\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Canteen Recharge System&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Canteen Recharge System&lt;\/h1&gt;<\/p>\n<p>    &lt;h2&gt;Register&lt;\/h2&gt;<\/p>\n<p>    &lt;form action=&quot;\/register&quot; method=&quot;post&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;username&quot;&gt;Username:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot; required&gt;<\/p>\n<p>        &lt;label for=&quot;password&quot;&gt;Password:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot; required&gt;<\/p>\n<p>        &lt;button type=&quot;submit&quot;&gt;Register&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>    &lt;h2&gt;Login&lt;\/h2&gt;<\/p>\n<p>    &lt;form action=&quot;\/login&quot; method=&quot;post&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;username&quot;&gt;Username:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;username&quot; name=&quot;username&quot; required&gt;<\/p>\n<p>        &lt;label for=&quot;password&quot;&gt;Password:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;password&quot; id=&quot;password&quot; name=&quot;password&quot; required&gt;<\/p>\n<p>        &lt;button type=&quot;submit&quot;&gt;Login&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u8bbe\u8ba1\u5145\u503c\u9875\u9762<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528HTML\/CSS\u6765\u8bbe\u8ba1\u7528\u6237\u5145\u503c\u9875\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Canteen Recharge System&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Canteen Recharge System&lt;\/h1&gt;<\/p>\n<p>    &lt;h2&gt;Recharge&lt;\/h2&gt;<\/p>\n<p>    &lt;form action=&quot;\/recharge&quot; method=&quot;post&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;user_id&quot;&gt;User ID:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;user_id&quot; name=&quot;user_id&quot; required&gt;<\/p>\n<p>        &lt;label for=&quot;amount&quot;&gt;Amount:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;number&quot; id=&quot;amount&quot; name=&quot;amount&quot; required&gt;<\/p>\n<p>        &lt;button type=&quot;submit&quot;&gt;Recharge&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>3. \u8bbe\u8ba1\u67e5\u8be2\u9875\u9762<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528HTML\/CSS\u6765\u8bbe\u8ba1\u67e5\u8be2\u4f59\u989d\u548c\u5145\u503c\u8bb0\u5f55\u7684\u9875\u9762\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;title&gt;Canteen Recharge System&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Canteen Recharge System&lt;\/h1&gt;<\/p>\n<p>    &lt;h2&gt;Balance&lt;\/h2&gt;<\/p>\n<p>    &lt;form action=&quot;\/balance&quot; method=&quot;get&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;user_id&quot;&gt;User ID:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;user_id&quot; name=&quot;user_id&quot; required&gt;<\/p>\n<p>        &lt;button type=&quot;submit&quot;&gt;Check Balance&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>    &lt;h2&gt;Transactions&lt;\/h2&gt;<\/p>\n<p>    &lt;form action=&quot;\/transactions&quot; method=&quot;get&quot;&gt;<\/p>\n<p>        &lt;label for=&quot;user_id&quot;&gt;User ID:&lt;\/label&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; id=&quot;user_id&quot; name=&quot;user_id&quot; required&gt;<\/p>\n<p>        &lt;button type=&quot;submit&quot;&gt;Check Transactions&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u6d4b\u8bd5\u548c\u90e8\u7f72<\/strong><\/h2>\n<p><h2>1. \u6d4b\u8bd5<\/h2>\n<\/p>\n<p><p>\u5728\u672c\u5730\u8fd0\u884cFlask\u5e94\u7528\uff0c\u5e76\u4f7f\u7528\u6d4f\u89c8\u5668\u6d4b\u8bd5\u5404\u4e2a\u529f\u80fd\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">python app.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8bbf\u95ee<code>http:\/\/127.0.0.1:5000\/<\/code>\uff0c\u6d4b\u8bd5\u6ce8\u518c\u3001\u767b\u5f55\u3001\u5145\u503c\u548c\u67e5\u8be2\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h2>2. \u90e8\u7f72<\/h2>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Gunicorn\u548cNginx\u6765\u90e8\u7f72Flask\u5e94\u7528\u5230\u751f\u4ea7\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u90e8\u7f72\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5Gunicorn<\/h3>\n<\/p>\n<p><pre><code class=\"language-sh\">pip install gunicorn<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u8fd0\u884cGunicorn<\/h3>\n<\/p>\n<p><pre><code class=\"language-sh\">gunicorn -w 4 -b 0.0.0.0:8000 app:app<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u914d\u7f6eNginx<\/h3>\n<\/p>\n<p><p>\u5728Nginx\u914d\u7f6e\u6587\u4ef6\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-nginx\">server {<\/p>\n<p>    listen 80;<\/p>\n<p>    server_name your_domain.com;<\/p>\n<p>    location \/ {<\/p>\n<p>        proxy_pass http:\/\/127.0.0.1:8000;<\/p>\n<p>        proxy_set_header Host $host;<\/p>\n<p>        proxy_set_header X-Real-IP $remote_addr;<\/p>\n<p>        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/p>\n<p>        proxy_set_header X-Forwarded-Proto $scheme;<\/p>\n<p>    }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u91cd\u542fNginx\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-sh\">sudo systemctl restart nginx<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u73b0\u5728\uff0c\u4f60\u7684\u996d\u5802\u5145\u503c\u7cfb\u7edf\u5df2\u7ecf\u6210\u529f\u90e8\u7f72\u5230\u751f\u4ea7\u73af\u5883\uff0c\u53ef\u4ee5\u901a\u8fc7\u57df\u540d\u8bbf\u95ee\u3002<\/p>\n<\/p>\n<h2><strong>\u603b\u7ed3<\/strong><\/h2>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u4ecb\u7ecd\uff0c\u6211\u4eec\u8be6\u7ec6\u8bb2\u89e3\u4e86\u5982\u4f55\u4f7f\u7528Python\u5f00\u53d1\u4e00\u4e2a\u996d\u5802\u5145\u503c\u7cfb\u7edf\u3002\u6211\u4eec\u4f7f\u7528Flask\u4f5c\u4e3aWeb\u6846\u67b6\uff0cSQLite\u4f5c\u4e3a\u6570\u636e\u5e93\uff0cHTML\/CSS\/JavaScript\u4f5c\u4e3a\u524d\u7aef\u5f00\u53d1\u5de5\u5177\uff0c\u5e76\u4e14\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u7528\u6237\u6ce8\u518c\u3001\u767b\u5f55\u3001\u5145\u503c\u3001\u67e5\u8be2\u4f59\u989d\u548c\u5145\u503c\u8bb0\u5f55\u7b49\u529f\u80fd\u7684\u5b9e\u73b0\u3002\u6700\u540e\uff0c\u6211\u4eec\u8fd8\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728\u672c\u5730\u6d4b\u8bd5\u548c\u90e8\u7f72\u8be5\u7cfb\u7edf\u3002\u5e0c\u671b\u8fd9\u7bc7\u6587\u7ae0\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728\u5f00\u53d1\u996d\u5802\u5145\u503c\u7cfb\u7edf\u65f6\uff0c\u6211\u5e94\u8be5\u9009\u62e9\u54ea\u79cd\u6570\u636e\u5e93\uff1f<\/strong><br \/>\u5728\u9009\u62e9\u6570\u636e\u5e93\u65f6\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u5173\u7cfb\u578b\u6570\u636e\u5e93\u5982MySQL\u6216PostgreSQL\uff0c\u8fd9\u4e9b\u6570\u636e\u5e93\u652f\u6301\u590d\u6742\u67e5\u8be2\u548c\u4e8b\u52a1\u5904\u7406\uff0c\u975e\u5e38\u9002\u5408\u7ba1\u7406\u7528\u6237\u4fe1\u606f\u548c\u5145\u503c\u8bb0\u5f55\u3002\u6b64\u5916\uff0cNoSQL\u6570\u636e\u5e93\u5982MongoDB\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u4e0d\u89c4\u5219\u6570\u636e\uff0c\u9009\u62e9\u65f6\u5e94\u6839\u636e\u9879\u76ee\u9700\u6c42\u548c\u56e2\u961f\u7684\u6280\u672f\u6808\u6765\u51b3\u5b9a\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u7528\u6237\u5145\u503c\u8fc7\u7a0b\u4e2d\u7684\u5b89\u5168\u6027\u95ee\u9898\uff1f<\/strong><br \/>\u786e\u4fdd\u7528\u6237\u6570\u636e\u548c\u4ea4\u6613\u7684\u5b89\u5168\u6027\u81f3\u5173\u91cd\u8981\u3002\u53ef\u4ee5\u91c7\u7528HTTPS\u534f\u8bae\u6765\u52a0\u5bc6\u6570\u636e\u4f20\u8f93\uff0c\u5e76\u4f7f\u7528\u7b2c\u4e09\u65b9\u652f\u4ed8\u5e73\u53f0\uff08\u5982\u652f\u4ed8\u5b9d\u3001\u5fae\u4fe1\u652f\u4ed8\u7b49\uff09\u6765\u5904\u7406\u652f\u4ed8\u73af\u8282\u3002\u540c\u65f6\uff0c\u5efa\u8bae\u5728\u7cfb\u7edf\u4e2d\u5b9e\u73b0\u5b89\u5168\u8ba4\u8bc1\u673a\u5236\uff0c\u6bd4\u5982\u4f7f\u7528JWT\uff08JSON Web Tokens\uff09\u6765\u9a8c\u8bc1\u7528\u6237\u8eab\u4efd\uff0c\u4ee5\u9632\u6b62\u672a\u6388\u6743\u8bbf\u95ee\u3002<\/p>\n<p><strong>\u5982\u4f55\u8bbe\u8ba1\u7528\u6237\u754c\u9762\u4ee5\u63d0\u9ad8\u5145\u503c\u4f53\u9a8c\uff1f<\/strong><br \/>\u8bbe\u8ba1\u65f6\u5e94\u6ce8\u91cd\u7b80\u6d01\u548c\u76f4\u89c2\u6027\uff0c\u53ef\u4ee5\u4f7f\u7528\u6d41\u884c\u7684\u524d\u7aef\u6846\u67b6\u5982React\u6216Vue.js\u6765\u6784\u5efa\u7528\u6237\u754c\u9762\u3002\u786e\u4fdd\u5145\u503c\u6d41\u7a0b\u7684\u6b65\u9aa4\u6e05\u6670\uff0c\u5e76\u63d0\u4f9b\u5b9e\u65f6\u53cd\u9988\uff0c\u4f8b\u5982\u5145\u503c\u6210\u529f\u63d0\u793a\u6216\u9519\u8bef\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u52a0\u5165\u591a\u79cd\u652f\u4ed8\u65b9\u5f0f\u548c\u5145\u503c\u91d1\u989d\u9009\u9879\uff0c\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u7684\u4fbf\u5229\u6027\u548c\u9009\u62e9\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u4f7f\u7528Python\u5f00\u53d1\u996d\u5802\u5145\u503c\u7cfb\u7edf\uff0c\u53ef\u4ee5\u4f7f\u7528Flask\u6216Django\u8fd9\u6837\u7684Web\u6846\u67b6\u3001\u4f7f\u7528SQLite\u6216MyS [&hellip;]","protected":false},"author":3,"featured_media":1177249,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177239"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1177239"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177239\/revisions"}],"predecessor-version":[{"id":1177252,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1177239\/revisions\/1177252"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1177249"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1177239"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1177239"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1177239"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}