{"id":1056488,"date":"2024-12-31T15:00:09","date_gmt":"2024-12-31T07:00:09","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056488.html"},"modified":"2024-12-31T15:00:12","modified_gmt":"2024-12-31T07:00:12","slug":"%e5%a6%82%e4%bd%95%e8%bf%9b%e8%a1%8c%e7%bd%91%e9%a1%b5%e4%b8%8epython%e7%9a%84%e4%ba%a4%e4%ba%92","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1056488.html","title":{"rendered":"\u5982\u4f55\u8fdb\u884c\u7f51\u9875\u4e0epython\u7684\u4ea4\u4e92"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/3b8fa95d-9fbc-4153-a843-2efbed114a98.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u8fdb\u884c\u7f51\u9875\u4e0epython\u7684\u4ea4\u4e92\" \/><\/p>\n<p><p> <strong>\u8fdb\u884c\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92\uff0c\u53ef\u4ee5\u4f7f\u7528Flask\u3001Django\u3001API\u3001JavaScript\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528Flask\u662f\u6700\u7b80\u5355\u3001\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\uff0c\u56e0\u4e3a\u5b83\u8f7b\u91cf\u4e14\u6613\u4e8e\u4e0a\u624b\u3002<strong>Flask\u662f\u4e00\u4e2a\u5fae\u578b\u6846\u67b6\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\u548c\u5feb\u901f\u5f00\u53d1\u3002<\/strong>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Flask\u8fdb\u884c\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001Flask\u6846\u67b6\u7684\u57fa\u672c\u4ecb\u7ecd<\/p>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u5fae\u578b\u7684Web\u6846\u67b6\uff0c\u4f7f\u7528Python\u7f16\u5199\uff0c\u65e8\u5728\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u642d\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u5b83\u7684\u6838\u5fc3\u6982\u5ff5\u5305\u62ec\u8def\u7531\uff08Routing\uff09\u3001\u6a21\u677f\uff08Templating\uff09\u548c\u8bf7\u6c42\uff08Request\uff09\u5904\u7406\u3002\u7531\u4e8eFlask\u975e\u5e38\u8f7b\u91cf\u7ea7\uff0c\u6240\u4ee5\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u6839\u636e\u9700\u6c42\u6dfb\u52a0\u6269\u5c55\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5\u4e0e\u57fa\u672c\u914d\u7f6e<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5Flask\u3002\u53ef\u4ee5\u901a\u8fc7pip\u547d\u4ee4\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684Python\u6587\u4ef6\uff0c\u5e76\u5bfc\u5165Flask\u6a21\u5757\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, request, render_template, jsonify<\/p>\n<p>app = Flask(__name__)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efa\u57fa\u672c\u8def\u7531<\/li>\n<\/ol>\n<p><p>\u5728Flask\u4e2d\uff0c\u8def\u7531\u7528\u4e8e\u5c06URL\u4e0e\u7279\u5b9a\u7684\u51fd\u6570\u7ed1\u5b9a\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/&#39;)<\/p>\n<p>def home():<\/p>\n<p>    return &quot;Hello, Flask!&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u5f53\u7528\u6237\u8bbf\u95ee\u6839URL\uff08<code>\/<\/code>\uff09\u65f6\uff0c\u5c06\u8c03\u7528<code>home<\/code>\u51fd\u6570\uff0c\u5e76\u8fd4\u56de\u5b57\u7b26\u4e32&quot;Hello, Flask!&quot;\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528HTML\u6a21\u677f\u8fdb\u884c\u4ea4\u4e92<\/p>\n<\/p>\n<p><p>\u4f7f\u7528HTML\u6a21\u677f\u53ef\u4ee5\u4f7f\u6211\u4eec\u521b\u5efa\u66f4\u52a0\u590d\u6742\u548c\u52a8\u6001\u7684\u7f51\u9875\u3002Flask\u9ed8\u8ba4\u4f7f\u7528Jinja2\u6a21\u677f\u5f15\u64ce\u6765\u6e32\u67d3HTML\u9875\u9762\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u6a21\u677f\u6587\u4ef6<\/li>\n<\/ol>\n<p><p>\u9996\u5148\uff0c\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>templates<\/code>\u7684\u6587\u4ef6\u5939\uff0c\u5e76\u5728\u5176\u4e2d\u521b\u5efa\u4e00\u4e2aHTML\u6587\u4ef6\uff0c\u4f8b\u5982<code>index.html<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;Flask Webpage&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Welcome to Flask Webpage&lt;\/h1&gt;<\/p>\n<p>    &lt;form action=&quot;\/process&quot; method=&quot;POST&quot;&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; name=&quot;data&quot; placeholder=&quot;Enter some data&quot;&gt;<\/p>\n<p>        &lt;button type=&quot;submit&quot;&gt;Submit&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<ol start=\"2\">\n<li>\u6e32\u67d3\u6a21\u677f<\/li>\n<\/ol>\n<p><p>\u5728Flask\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528<code>render_template<\/code>\u51fd\u6570\u6765\u6e32\u67d3HTML\u6a21\u677f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/&#39;)<\/p>\n<p>def home():<\/p>\n<p>    return render_template(&#39;index.html&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5904\u7406\u8868\u5355\u63d0\u4ea4<\/p>\n<\/p>\n<p><p>\u4e3a\u4e86\u5904\u7406\u7528\u6237\u63d0\u4ea4\u7684\u6570\u636e\uff0c\u6211\u4eec\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u8def\u7531\u6765\u63a5\u6536\u548c\u5904\u7406\u8868\u5355\u6570\u636e\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efa\u5904\u7406\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u5728Flask\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>request<\/code>\u5bf9\u8c61\u6765\u83b7\u53d6\u8868\u5355\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/process&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def process():<\/p>\n<p>    data = request.form[&#39;data&#39;]<\/p>\n<p>    # \u5728\u8fd9\u91cc\u5904\u7406\u6570\u636e<\/p>\n<p>    return f&quot;Received data: {data}&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u8fd4\u56deJSON\u54cd\u5e94<\/li>\n<\/ol>\n<p><p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u53ef\u80fd\u5e0c\u671b\u8fd4\u56deJSON\u683c\u5f0f\u7684\u6570\u636e\uff0c\u4f8b\u5982\u5728\u5904\u7406AJAX\u8bf7\u6c42\u65f6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/process&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def process():<\/p>\n<p>    data = request.form[&#39;data&#39;]<\/p>\n<p>    # \u5728\u8fd9\u91cc\u5904\u7406\u6570\u636e<\/p>\n<p>    response = {<\/p>\n<p>        &#39;status&#39;: &#39;success&#39;,<\/p>\n<p>        &#39;data&#39;: data<\/p>\n<p>    }<\/p>\n<p>    return jsonify(response)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528AJAX\u8fdb\u884c\u5f02\u6b65\u8bf7\u6c42<\/p>\n<\/p>\n<p><p>AJAX\u662f\u4e00\u79cd\u5728\u4e0d\u91cd\u65b0\u52a0\u8f7d\u6574\u4e2a\u7f51\u9875\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0e\u670d\u52a1\u5668\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u7684\u6280\u672f\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528JavaScript\u4e2d\u7684<code>fetch<\/code>\u51fd\u6570\u6765\u53d1\u9001AJAX\u8bf7\u6c42\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5728HTML\u6a21\u677f\u4e2d\u6dfb\u52a0AJAX\u4ee3\u7801<\/li>\n<\/ol>\n<p><p>\u4fee\u6539<code>index.html<\/code>\uff0c\u6dfb\u52a0\u4e00\u6bb5JavaScript\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;Flask Webpage&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Welcome to Flask Webpage&lt;\/h1&gt;<\/p>\n<p>    &lt;form id=&quot;dataForm&quot;&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; name=&quot;data&quot; id=&quot;dat<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>nput&quot; placeholder=&quot;Enter some data&quot;&gt;<\/p>\n<p>        &lt;button type=&quot;button&quot; onclick=&quot;sendData()&quot;&gt;Submit&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>    &lt;div id=&quot;response&quot;&gt;&lt;\/div&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        function sendData() {<\/p>\n<p>            const data = document.getElementById(&#39;dataInput&#39;).value;<\/p>\n<p>            fetch(&#39;\/process&#39;, {<\/p>\n<p>                method: &#39;POST&#39;,<\/p>\n<p>                headers: {<\/p>\n<p>                    &#39;Content-Type&#39;: &#39;application\/json&#39;<\/p>\n<p>                },<\/p>\n<p>                body: JSON.stringify({ data: data })<\/p>\n<p>            })<\/p>\n<p>            .then(response =&gt; response.json())<\/p>\n<p>            .then(data =&gt; {<\/p>\n<p>                document.getElementById(&#39;response&#39;).innerText = `Received data: ${data.data}`;<\/p>\n<p>            });<\/p>\n<p>        }<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5904\u7406JSON\u8bf7\u6c42<\/li>\n<\/ol>\n<p><p>\u5728Flask\u4e2d\uff0c\u4f7f\u7528<code>request.get_json()<\/code>\u6765\u83b7\u53d6JSON\u683c\u5f0f\u7684\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/process&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def process():<\/p>\n<p>    data = request.get_json()[&#39;data&#39;]<\/p>\n<p>    # \u5728\u8fd9\u91cc\u5904\u7406\u6570\u636e<\/p>\n<p>    response = {<\/p>\n<p>        &#39;status&#39;: &#39;success&#39;,<\/p>\n<p>        &#39;data&#39;: data<\/p>\n<p>    }<\/p>\n<p>    return jsonify(response)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4e0e\u6570\u636e\u5e93\u4ea4\u4e92<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\u3002Flask\u53ef\u4ee5\u4e0e\u591a\u79cd\u6570\u636e\u5e93\u8fdb\u884c\u4ea4\u4e92\uff0c\u8fd9\u91cc\u4ee5SQLite\u4e3a\u4f8b\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5SQLAlchemy<\/li>\n<\/ol>\n<p><p>SQLAlchemy\u662f\u4e00\u4e2a\u6d41\u884c\u7684ORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5e93\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u4f7f\u7528Python\u7c7b\u6765\u64cd\u4f5c\u6570\u636e\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5SQLAlchemy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install SQLAlchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u914d\u7f6e\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p><p>\u5728Flask\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u5bfc\u5165\u5e76\u914d\u7f6eSQLAlchemy\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask_sqlalchemy import SQLAlchemy<\/p>\n<p>app.config[&#39;SQLALCHEMY_DATABASE_URI&#39;] = &#39;sqlite:\/\/\/data.db&#39;<\/p>\n<p>db = SQLAlchemy(app)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u521b\u5efa\u6a21\u578b<\/li>\n<\/ol>\n<p><p>\u5b9a\u4e49\u4e00\u4e2a\u6570\u636e\u5e93\u6a21\u578b\u7c7b\uff0c\u4f8b\u5982User\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">class User(db.Model):<\/p>\n<p>    id = db.Column(db.Integer, primary_key=True)<\/p>\n<p>    name = db.Column(db.String(80), nullable=False)<\/p>\n<p>    def __repr__(self):<\/p>\n<p>        return f&#39;&lt;User {self.name}&gt;&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u521b\u5efa\u6570\u636e\u5e93\u8868<\/li>\n<\/ol>\n<p><p>\u5728Python shell\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u521b\u5efa\u6570\u636e\u5e93\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from your_flask_app import db<\/p>\n<p>db.create_all()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li>\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93<\/li>\n<\/ol>\n<p><p>\u5728\u5904\u7406\u8868\u5355\u63d0\u4ea4\u65f6\uff0c\u5c06\u6570\u636e\u5b58\u50a8\u5230\u6570\u636e\u5e93\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/process&#39;, methods=[&#39;POST&#39;])<\/p>\n<p>def process():<\/p>\n<p>    data = request.get_json()[&#39;data&#39;]<\/p>\n<p>    new_user = User(name=data)<\/p>\n<p>    db.session.add(new_user)<\/p>\n<p>    db.session.commit()<\/p>\n<p>    response = {<\/p>\n<p>        &#39;status&#39;: &#39;success&#39;,<\/p>\n<p>        &#39;data&#39;: data<\/p>\n<p>    }<\/p>\n<p>    return jsonify(response)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528Django\u8fdb\u884c\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92<\/p>\n<\/p>\n<p><p>\u9664\u4e86Flask\uff0cDjango\u662f\u53e6\u4e00\u4e2a\u6d41\u884c\u7684Python Web\u6846\u67b6\u3002Django\u66f4\u4e3a\u5f3a\u5927\uff0c\u9002\u7528\u4e8e\u5927\u578b\u9879\u76ee\u3002\u4e0b\u9762\u7b80\u8981\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Django\u8fdb\u884c\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5Django<\/li>\n<\/ol>\n<p><p>\u53ef\u4ee5\u4f7f\u7528pip\u547d\u4ee4\u5b89\u88c5Django\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install Django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u521b\u5efaDjango\u9879\u76ee<\/li>\n<\/ol>\n<p><p>\u4f7f\u7528Django\u547d\u4ee4\u884c\u5de5\u5177\u521b\u5efa\u4e00\u4e2a\u65b0\u9879\u76ee\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">django-admin startproject myproject<\/p>\n<p>cd myproject<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li>\u521b\u5efaDjango\u5e94\u7528<\/li>\n<\/ol>\n<p><p>\u5728\u9879\u76ee\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u5e94\u7528\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li>\u914d\u7f6eURLs<\/li>\n<\/ol>\n<p><p>\u5728<code>myapp<\/code>\u76ee\u5f55\u4e0b\u7684<code>urls.py<\/code>\u6587\u4ef6\u4e2d\u5b9a\u4e49URL\u6a21\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.urls import path<\/p>\n<p>from . import views<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;&#39;, views.home, name=&#39;home&#39;),<\/p>\n<p>    path(&#39;process\/&#39;, views.process, name=&#39;process&#39;),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"5\">\n<li>\u7f16\u5199\u89c6\u56fe\u51fd\u6570<\/li>\n<\/ol>\n<p><p>\u5728<code>views.py<\/code>\u6587\u4ef6\u4e2d\u5b9a\u4e49\u89c6\u56fe\u51fd\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.shortcuts import render<\/p>\n<p>from django.http import JsonResponse<\/p>\n<p>def home(request):<\/p>\n<p>    return render(request, &#39;index.html&#39;)<\/p>\n<p>def process(request):<\/p>\n<p>    data = request.POST.get(&#39;data&#39;)<\/p>\n<p>    response = {<\/p>\n<p>        &#39;status&#39;: &#39;success&#39;,<\/p>\n<p>        &#39;data&#39;: data<\/p>\n<p>    }<\/p>\n<p>    return JsonResponse(response)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"6\">\n<li>\u521b\u5efaHTML\u6a21\u677f<\/li>\n<\/ol>\n<p><p>\u5728<code>templates<\/code>\u76ee\u5f55\u4e0b\u521b\u5efa<code>index.html<\/code>\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u8868\u5355\u548cAJAX\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;Django Webpage&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Welcome to Django Webpage&lt;\/h1&gt;<\/p>\n<p>    &lt;form id=&quot;dataForm&quot;&gt;<\/p>\n<p>        &lt;input type=&quot;text&quot; name=&quot;data&quot; id=&quot;dataInput&quot; placeholder=&quot;Enter some data&quot;&gt;<\/p>\n<p>        &lt;button type=&quot;button&quot; onclick=&quot;sendData()&quot;&gt;Submit&lt;\/button&gt;<\/p>\n<p>    &lt;\/form&gt;<\/p>\n<p>    &lt;div id=&quot;response&quot;&gt;&lt;\/div&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        function sendData() {<\/p>\n<p>            const data = document.getElementById(&#39;dataInput&#39;).value;<\/p>\n<p>            fetch(&#39;\/process\/&#39;, {<\/p>\n<p>                method: &#39;POST&#39;,<\/p>\n<p>                headers: {<\/p>\n<p>                    &#39;Content-Type&#39;: &#39;application\/json&#39;<\/p>\n<p>                },<\/p>\n<p>                body: JSON.stringify({ data: data })<\/p>\n<p>            })<\/p>\n<p>            .then(response =&gt; response.json())<\/p>\n<p>            .then(data =&gt; {<\/p>\n<p>                document.getElementById(&#39;response&#39;).innerText = `Received data: ${data.data}`;<\/p>\n<p>            });<\/p>\n<p>        }<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528Django\u8fdb\u884c\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528API\u8fdb\u884c\u4ea4\u4e92<\/p>\n<\/p>\n<p><p>API\uff08\u5e94\u7528\u7a0b\u5e8f\u7f16\u7a0b\u63a5\u53e3\uff09\u662f\u4e00\u79cd\u5141\u8bb8\u4e0d\u540c\u8f6f\u4ef6\u7cfb\u7edf\u4e4b\u95f4\u8fdb\u884c\u901a\u4fe1\u7684\u673a\u5236\u3002\u901a\u8fc7API\uff0c\u6211\u4eec\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<ol>\n<li>\u521b\u5efaAPI\u7aef\u70b9<\/li>\n<\/ol>\n<p><p>\u5728Flask\u5e94\u7528\u7a0b\u5e8f\u4e2d\u521b\u5efa\u4e00\u4e2aAPI\u7aef\u70b9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">@app.route(&#39;\/api\/data&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<\/p>\n<p>def api_data():<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        data = request.get_json()[&#39;data&#39;]<\/p>\n<p>        response = {<\/p>\n<p>            &#39;status&#39;: &#39;success&#39;,<\/p>\n<p>            &#39;data&#39;: data<\/p>\n<p>        }<\/p>\n<p>        return jsonify(response)<\/p>\n<p>    else:<\/p>\n<p>        return jsonify({&#39;message&#39;: &#39;Send a POST request with JSON data&#39;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f7f\u7528JavaScript\u8c03\u7528API<\/li>\n<\/ol>\n<p><p>\u5728HTML\u6a21\u677f\u4e2d\u4f7f\u7528JavaScript\u4ee3\u7801\u8c03\u7528API\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-html\">&lt;!DOCTYPE html&gt;<\/p>\n<p>&lt;html lang=&quot;en&quot;&gt;<\/p>\n<p>&lt;head&gt;<\/p>\n<p>    &lt;meta charset=&quot;UTF-8&quot;&gt;<\/p>\n<p>    &lt;title&gt;API Interaction&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;API Interaction Example&lt;\/h1&gt;<\/p>\n<p>    &lt;input type=&quot;text&quot; id=&quot;apiData&quot; placeholder=&quot;Enter data&quot;&gt;<\/p>\n<p>    &lt;button onclick=&quot;callAPI()&quot;&gt;Send&lt;\/button&gt;<\/p>\n<p>    &lt;div id=&quot;apiResponse&quot;&gt;&lt;\/div&gt;<\/p>\n<p>    &lt;script&gt;<\/p>\n<p>        function callAPI() {<\/p>\n<p>            const data = document.getElementById(&#39;apiData&#39;).value;<\/p>\n<p>            fetch(&#39;\/api\/data&#39;, {<\/p>\n<p>                method: &#39;POST&#39;,<\/p>\n<p>                headers: {<\/p>\n<p>                    &#39;Content-Type&#39;: &#39;application\/json&#39;<\/p>\n<p>                },<\/p>\n<p>                body: JSON.stringify({ data: data })<\/p>\n<p>            })<\/p>\n<p>            .then(response =&gt; response.json())<\/p>\n<p>            .then(data =&gt; {<\/p>\n<p>                document.getElementById(&#39;apiResponse&#39;).innerText = `Response: ${data.data}`;<\/p>\n<p>            });<\/p>\n<p>        }<\/p>\n<p>    &lt;\/script&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7API\uff0c\u60a8\u53ef\u4ee5\u5b9e\u73b0\u66f4\u7075\u6d3b\u548c\u53ef\u6269\u5c55\u7684\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><p>\u516b\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u672c\u6587\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u4f7f\u7528Flask\u3001Django\u3001API\u548cJavaScript\u8fdb\u884c\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92\u3002\u901a\u8fc7\u5b66\u4e60\u8fd9\u4e9b\u6280\u672f\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u7f51\u9875\u4e0e\u540e\u7aefPython\u4ee3\u7801\u4e4b\u95f4\u7684\u6570\u636e\u4ea4\u6362\u548c\u4ea4\u4e92\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u6846\u67b6\u548c\u5de5\u5177\u53ef\u4ee5\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u548c\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u60a8\u5728Web\u5f00\u53d1\u7684\u9053\u8def\u4e0a\u53d6\u5f97\u6210\u529f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5b9e\u73b0\u7f51\u9875\u6570\u636e\u7684\u52a8\u6001\u52a0\u8f7d\uff1f<\/strong><br \/>\u4f7f\u7528Python\u4e0e\u7f51\u9875\u8fdb\u884c\u4ea4\u4e92\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7Flask\u6216Django\u7b49Web\u6846\u67b6\u521b\u5efaAPI\u3002\u901a\u8fc7AJAX\u6280\u672f\uff0c\u7f51\u9875\u53ef\u4ee5\u5411\u540e\u7aef\u53d1\u9001\u8bf7\u6c42\uff0c\u83b7\u53d6\u6570\u636e\u5e76\u52a8\u6001\u66f4\u65b0\u9875\u9762\u5185\u5bb9\u3002\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u63d0\u9ad8\u7528\u6237\u4f53\u9a8c\uff0c\u51cf\u5c11\u9875\u9762\u7684\u5237\u65b0\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u7f51\u9875\u8868\u5355\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u7684Web\u6846\u67b6\u4e2d\uff0c\u8868\u5355\u6570\u636e\u7684\u5904\u7406\u901a\u5e38\u901a\u8fc7HTTP POST\u8bf7\u6c42\u5b9e\u73b0\u3002\u7528\u6237\u5728\u7f51\u9875\u4e0a\u63d0\u4ea4\u8868\u5355\u540e\uff0c\u540e\u7aef\u63a5\u6536\u6570\u636e\u5e76\u8fdb\u884c\u9a8c\u8bc1\u6216\u5b58\u50a8\u3002\u4f7f\u7528Flask\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>request.form<\/code>\u6765\u83b7\u53d6\u8868\u5355\u4e2d\u7684\u6570\u636e\uff0c\u800cDjango\u5219\u4f7f\u7528<code>request.POST<\/code>\u6765\u5904\u7406\u76f8\u5e94\u7684\u5185\u5bb9\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728\u7f51\u9875\u4e0a\u663e\u793aPython\u5904\u7406\u7684\u7ed3\u679c\uff1f<\/strong><br \/>\u8981\u5728\u7f51\u9875\u4e0a\u5c55\u793aPython\u5904\u7406\u7684\u7ed3\u679c\uff0c\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u5f15\u64ce\uff0c\u5982Jinja2\uff08Flask\u7684\u9ed8\u8ba4\u6a21\u677f\u5f15\u64ce\uff09\u6216Django\u81ea\u5e26\u7684\u6a21\u677f\u7cfb\u7edf\u3002\u5904\u7406\u540e\u7684\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u6e32\u67d3\u6a21\u677f\u4f20\u9012\u5230\u7f51\u9875\u4e0a\uff0c\u7528\u6237\u4fbf\u53ef\u4ee5\u770b\u5230Python\u4ee3\u7801\u6267\u884c\u7684\u7ed3\u679c\uff0c\u4ece\u800c\u5b9e\u73b0\u524d\u540e\u7aef\u7684\u6709\u6548\u4ea4\u4e92\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8fdb\u884c\u7f51\u9875\u4e0ePython\u7684\u4ea4\u4e92\uff0c\u53ef\u4ee5\u4f7f\u7528Flask\u3001Django\u3001API\u3001JavaScript\u3002\u5176\u4e2d\uff0c\u4f7f\u7528Fla [&hellip;]","protected":false},"author":3,"featured_media":1056497,"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\/1056488"}],"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=1056488"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056488\/revisions"}],"predecessor-version":[{"id":1056501,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056488\/revisions\/1056501"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056497"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}