{"id":1046518,"date":"2024-12-31T13:34:08","date_gmt":"2024-12-31T05:34:08","guid":{"rendered":""},"modified":"2024-12-31T13:34:12","modified_gmt":"2024-12-31T05:34:12","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e5%81%9a%e7%bd%91%e7%ab%99%e8%84%9a%e6%9c%ac%e8%af%ad%e8%a8%80","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1046518.html","title":{"rendered":"\u5982\u4f55\u7528python\u505a\u7f51\u7ad9\u811a\u672c\u8bed\u8a00"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/fa661cc4-22a8-44ee-b8b3-1a3b8981bb33.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"\u5982\u4f55\u7528python\u505a\u7f51\u7ad9\u811a\u672c\u8bed\u8a00\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u4f5c\u4e3a\u7f51\u7ad9\u811a\u672c\u8bed\u8a00\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Web\u6846\u67b6\u3001\u5904\u7406HTTP\u8bf7\u6c42\u3001\u7ba1\u7406\u4f1a\u8bdd\u3001\u6a21\u677f\u6e32\u67d3\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u7b49\u3002<\/strong>\u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528Web\u6846\u67b6\u5982Django\u548cFlask\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u4eec\u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u529f\u80fd\uff0c\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efa\u590d\u6742\u7684Web\u5e94\u7528\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528Web\u6846\u67b6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Web\u6846\u67b6\u662fPython\u5f00\u53d1Web\u5e94\u7528\u7a0b\u5e8f\u7684\u4e3b\u8981\u65b9\u6cd5\u3002Web\u6846\u67b6\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7684\u5de5\u5177\u548c\u5e93\uff0c\u4f7f\u5f00\u53d1\u8005\u80fd\u591f\u5feb\u901f\u548c\u9ad8\u6548\u5730\u6784\u5efaWeb\u5e94\u7528\u7a0b\u5e8f\u3002\u6700\u6d41\u884c\u7684Python Web\u6846\u67b6\u5305\u62ecDjango\u548cFlask\u3002<\/p>\n<\/p>\n<p><h4>1. Django<\/h4>\n<\/p>\n<p><p>Django\u662f\u4e00\u4e2a\u9ad8\u7ea7Python Web\u6846\u67b6\uff0c\u9f13\u52b1\u5feb\u901f\u5f00\u53d1\u548c\u5e72\u51c0\u3001\u5b9e\u7528\u7684\u8bbe\u8ba1\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Django\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Django<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install django<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u9879\u76ee<\/strong>\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<\/li>\n<li>\n<p><strong>\u521b\u5efa\u5e94\u7528<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py startapp myapp<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b9a\u4e49\u6a21\u578b<\/strong>\uff1a<\/p>\n<p> \u5728<code>myapp\/models.py<\/code>\u4e2d\u5b9a\u4e49\u6570\u636e\u5e93\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class Article(models.Model):<\/p>\n<p>    title = models.CharField(max_length=100)<\/p>\n<p>    content = models.TextField()<\/p>\n<p>    published_date = models.DateTimeField(auto_now_add=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u89c6\u56fe<\/strong>\uff1a<\/p>\n<p> \u5728<code>myapp\/views.py<\/code>\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 .models import Article<\/p>\n<p>def index(request):<\/p>\n<p>    articles = Article.objects.all()<\/p>\n<p>    return render(request, &#39;index.html&#39;, {&#39;articles&#39;: articles})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6eURL<\/strong>\uff1a<\/p>\n<p> \u5728<code>myproject\/urls.py<\/code>\u4e2d\u914d\u7f6eURL\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib import admin<\/p>\n<p>from django.urls import path<\/p>\n<p>from myapp import views<\/p>\n<p>urlpatterns = [<\/p>\n<p>    path(&#39;admin\/&#39;, admin.site.urls),<\/p>\n<p>    path(&#39;&#39;, views.index),<\/p>\n<p>]<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u6a21\u677f<\/strong>\uff1a<\/p>\n<p> \u5728<code>myapp\/templates\/index.html<\/code>\u4e2d\u521b\u5efa\u6a21\u677f\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;Articles&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Articles&lt;\/h1&gt;<\/p>\n<p>    &lt;ul&gt;<\/p>\n<p>        {% for article in articles %}<\/p>\n<p>            &lt;li&gt;{{ article.title }} - {{ article.published_date }}&lt;\/li&gt;<\/p>\n<p>        {% endfor %}<\/p>\n<p>    &lt;\/ul&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u5f00\u53d1\u670d\u52a1\u5668<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python manage.py runserver<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Django\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Web\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h4>2. Flask<\/h4>\n<\/p>\n<p><p>Flask\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684Web\u6846\u67b6\uff0c\u9002\u5408\u5c0f\u578b\u9879\u76ee\u548c\u539f\u578b\u5f00\u53d1\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528Flask\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Flask<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flask<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u5e94\u7528<\/strong>\uff1a<\/p>\n<p> \u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa<code>app.py<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def index():<\/p>\n<p>    articles = [<\/p>\n<p>        {&#39;title&#39;: &#39;First Article&#39;, &#39;published_date&#39;: &#39;2021-01-01&#39;},<\/p>\n<p>        {&#39;title&#39;: &#39;Second Article&#39;, &#39;published_date&#39;: &#39;2021-02-01&#39;}<\/p>\n<p>    ]<\/p>\n<p>    return render_template(&#39;index.html&#39;, articles=articles)<\/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>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u521b\u5efa\u6a21\u677f<\/strong>\uff1a<\/p>\n<p> \u5728\u9879\u76ee\u76ee\u5f55\u4e2d\u521b\u5efa<code>templates\/index.html<\/code>\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;Articles&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Articles&lt;\/h1&gt;<\/p>\n<p>    &lt;ul&gt;<\/p>\n<p>        {% for article in articles %}<\/p>\n<p>            &lt;li&gt;{{ article.title }} - {{ article.published_date }}&lt;\/li&gt;<\/p>\n<p>        {% endfor %}<\/p>\n<p>    &lt;\/ul&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884c\u5e94\u7528<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">python app.py<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Flask\u5feb\u901f\u642d\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Web\u5e94\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5904\u7406HTTP\u8bf7\u6c42<\/h3>\n<\/p>\n<p><p>\u65e0\u8bba\u4f7f\u7528\u54ea\u4e2aWeb\u6846\u67b6\uff0c\u5904\u7406HTTP\u8bf7\u6c42\u90fd\u662fWeb\u5f00\u53d1\u7684\u6838\u5fc3\u4efb\u52a1\u4e4b\u4e00\u3002\u5904\u7406HTTP\u8bf7\u6c42\u5305\u62ec\u89e3\u6790\u5ba2\u6237\u7aef\u53d1\u9001\u7684\u6570\u636e\u3001\u9a8c\u8bc1\u6570\u636e\u3001\u6267\u884c\u4e1a\u52a1\u903b\u8f91\u3001\u751f\u6210\u54cd\u5e94\u7b49\u3002<\/p>\n<\/p>\n<p><h4>1. Django<\/h4>\n<\/p>\n<p><p>\u5728Django\u4e2d\uff0c\u5904\u7406HTTP\u8bf7\u6c42\u4e3b\u8981\u901a\u8fc7\u89c6\u56fe\u51fd\u6570\uff08\u6216\u7c7b\u89c6\u56fe\uff09\u6765\u5b8c\u6210\u3002\u89c6\u56fe\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a<code>HttpRequest<\/code>\u5bf9\u8c61\uff0c\u5e76\u8fd4\u56de\u4e00\u4e2a<code>HttpResponse<\/code>\u5bf9\u8c61\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.http import HttpResponse<\/p>\n<p>def my_view(request):<\/p>\n<p>    return HttpResponse(&#39;Hello, World!&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Flask<\/h4>\n<\/p>\n<p><p>\u5728Flask\u4e2d\uff0c\u5904\u7406HTTP\u8bf7\u6c42\u4e3b\u8981\u901a\u8fc7\u8def\u7531\u548c\u89c6\u56fe\u51fd\u6570\u6765\u5b8c\u6210\u3002\u89c6\u56fe\u51fd\u6570\u901a\u8fc7\u88c5\u9970\u5668\uff08\u5982<code>@app.route<\/code>\uff09\u6765\u7ed1\u5b9aURL\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def my_view():<\/p>\n<p>    return &#39;Hello, World!&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u7ba1\u7406\u4f1a\u8bdd<\/h3>\n<\/p>\n<p><p>\u4f1a\u8bdd\u7ba1\u7406\u662fWeb\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u90e8\u5206\uff0c\u5b83\u5141\u8bb8\u670d\u52a1\u5668\u5728\u591a\u4e2a\u8bf7\u6c42\u4e4b\u95f4\u4fdd\u6301\u7528\u6237\u7684\u72b6\u6001\u3002Python Web\u6846\u67b6\u901a\u5e38\u63d0\u4f9b\u5185\u7f6e\u7684\u4f1a\u8bdd\u7ba1\u7406\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h4>1. Django<\/h4>\n<\/p>\n<p><p>\u5728Django\u4e2d\uff0c\u4f1a\u8bdd\u7ba1\u7406\u7531<code>django.contrib.sessions<\/code>\u5e94\u7528\u63d0\u4f9b\u3002\u4f60\u53ef\u4ee5\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528<code>request.session<\/code>\u6765\u5b58\u50a8\u548c\u68c0\u7d22\u4f1a\u8bdd\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def my_view(request):<\/p>\n<p>    request.session[&#39;username&#39;] = &#39;john_doe&#39;<\/p>\n<p>    return HttpResponse(&#39;Username saved in session.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Flask<\/h4>\n<\/p>\n<p><p>\u5728Flask\u4e2d\uff0c\u4f1a\u8bdd\u7ba1\u7406\u7531<code>flask.session<\/code>\u6a21\u5757\u63d0\u4f9b\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>session<\/code>\u5bf9\u8c61\u6765\u5b58\u50a8\u548c\u68c0\u7d22\u4f1a\u8bdd\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, session<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>app.secret_key = &#39;super_secret_key&#39;<\/p>\n<p>@app.route(&#39;\/set_session&#39;)<\/p>\n<p>def set_session():<\/p>\n<p>    session[&#39;username&#39;] = &#39;john_doe&#39;<\/p>\n<p>    return &#39;Username saved in session.&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6a21\u677f\u6e32\u67d3<\/h3>\n<\/p>\n<p><p>\u6a21\u677f\u6e32\u67d3\u662fWeb\u5f00\u53d1\u4e2d\u5c06\u6570\u636e\u63d2\u5165HTML\u6a21\u677f\u5e76\u751f\u6210\u6700\u7ec8HTML\u9875\u9762\u7684\u8fc7\u7a0b\u3002Python Web\u6846\u67b6\u901a\u5e38\u63d0\u4f9b\u5185\u7f6e\u7684\u6a21\u677f\u5f15\u64ce\u3002<\/p>\n<\/p>\n<p><h4>1. Django<\/h4>\n<\/p>\n<p><p>Django\u5185\u7f6e\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u6a21\u677f\u5f15\u64ce\u3002\u4f60\u53ef\u4ee5\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528<code>render<\/code>\u51fd\u6570\u6765\u6e32\u67d3\u6a21\u677f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.shortcuts import render<\/p>\n<p>def my_view(request):<\/p>\n<p>    context = {&#39;username&#39;: &#39;john_doe&#39;}<\/p>\n<p>    return render(request, &#39;my_template.html&#39;, context)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6a21\u677f\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6a21\u677f\u8bed\u8a00\u6765\u63d2\u5165\u6570\u636e\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;My Template&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Hello, {{ username }}!&lt;\/h1&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Flask<\/h4>\n<\/p>\n<p><p>Flask\u4f7f\u7528Jinja2\u4f5c\u4e3a\u6a21\u677f\u5f15\u64ce\u3002\u4f60\u53ef\u4ee5\u5728\u89c6\u56fe\u4e2d\u4f7f\u7528<code>render_template<\/code>\u51fd\u6570\u6765\u6e32\u67d3\u6a21\u677f\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def my_view():<\/p>\n<p>    return render_template(&#39;my_template.html&#39;, username=&#39;john_doe&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6a21\u677f\u4e2d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528Jinja2\u6a21\u677f\u8bed\u8a00\u6765\u63d2\u5165\u6570\u636e\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;My Template&lt;\/title&gt;<\/p>\n<p>&lt;\/head&gt;<\/p>\n<p>&lt;body&gt;<\/p>\n<p>    &lt;h1&gt;Hello, {{ username }}!&lt;\/h1&gt;<\/p>\n<p>&lt;\/body&gt;<\/p>\n<p>&lt;\/html&gt;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u6570\u636e\u5e93\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5e93\u64cd\u4f5c\u662fWeb\u5e94\u7528\u7a0b\u5e8f\u7684\u6838\u5fc3\u529f\u80fd\u4e4b\u4e00\u3002Python Web\u6846\u67b6\u901a\u5e38\u63d0\u4f9bORM\uff08\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\uff09\u5de5\u5177\u6765\u7b80\u5316\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h4>1. Django<\/h4>\n<\/p>\n<p><p>Django\u5185\u7f6e\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684ORM\u5de5\u5177\u3002\u4f60\u53ef\u4ee5\u5728\u6a21\u578b\u7c7b\u4e2d\u5b9a\u4e49\u6570\u636e\u5e93\u8868\uff0c\u5e76\u4f7f\u7528Django\u7684ORM API\u6765\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.db import models<\/p>\n<p>class Article(models.Model):<\/p>\n<p>    title = models.CharField(max_length=100)<\/p>\n<p>    content = models.TextField()<\/p>\n<h2><strong>\u5728\u89c6\u56fe\u4e2d\u67e5\u8be2\u6570\u636e\u5e93<\/strong><\/h2>\n<p>from .models import Article<\/p>\n<p>def my_view(request):<\/p>\n<p>    articles = Article.objects.all()<\/p>\n<p>    return render(request, &#39;my_template.html&#39;, {&#39;articles&#39;: articles})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Flask<\/h4>\n<\/p>\n<p><p>Flask\u901a\u5e38\u4f7f\u7528SQLAlchemy\u4f5c\u4e3aORM\u5de5\u5177\u3002\u4f60\u9700\u8981\u5b89\u88c5Flask-SQLAlchemy\u6269\u5c55\u6765\u4f7f\u7528SQLAlchemy\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Flask-SQLAlchemy<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flask-sqlalchemy<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u5e94\u7528<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask<\/p>\n<p>from flask_sqlalchemy import SQLAlchemy<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>app.config[&#39;SQLALCHEMY_DATABASE_URI&#39;] = &#39;sqlite:\/\/\/mydatabase.db&#39;<\/p>\n<p>db = SQLAlchemy(app)<\/p>\n<p>class Article(db.Model):<\/p>\n<p>    id = db.Column(db.Integer, primary_key=True)<\/p>\n<p>    title = db.Column(db.String(100))<\/p>\n<p>    content = db.Column(db.Text)<\/p>\n<p>db.create_all()<\/p>\n<h2><strong>\u5728\u89c6\u56fe\u4e2d\u67e5\u8be2\u6570\u636e\u5e93<\/strong><\/h2>\n<p>@app.route(&#39;\/&#39;)<\/p>\n<p>def my_view():<\/p>\n<p>    articles = Article.query.all()<\/p>\n<p>    return render_template(&#39;my_template.html&#39;, articles=articles)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528SQLAlchemy\u5728Flask\u4e2d\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u8868\u5355\u5904\u7406\u548c\u9a8c\u8bc1<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u548c\u9a8c\u8bc1\u8868\u5355\u6570\u636e\u662fWeb\u5f00\u53d1\u4e2d\u7684\u91cd\u8981\u90e8\u5206\u3002Python Web\u6846\u67b6\u901a\u5e38\u63d0\u4f9b\u5185\u7f6e\u7684\u8868\u5355\u5904\u7406\u548c\u9a8c\u8bc1\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1. Django<\/h4>\n<\/p>\n<p><p>Django\u5185\u7f6e\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u8868\u5355\u5904\u7406\u548c\u9a8c\u8bc1\u6846\u67b6\u3002\u4f60\u53ef\u4ee5\u5b9a\u4e49\u8868\u5355\u7c7b\uff0c\u5e76\u5728\u89c6\u56fe\u4e2d\u5904\u7406\u548c\u9a8c\u8bc1\u8868\u5355\u6570\u636e\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django import forms<\/p>\n<p>class ArticleForm(forms.Form):<\/p>\n<p>    title = forms.CharField(max_length=100)<\/p>\n<p>    content = forms.CharField(widget=forms.Textarea)<\/p>\n<h2><strong>\u5728\u89c6\u56fe\u4e2d\u5904\u7406\u8868\u5355<\/strong><\/h2>\n<p>from django.shortcuts import render<\/p>\n<p>def my_view(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        form = ArticleForm(request.POST)<\/p>\n<p>        if form.is_valid():<\/p>\n<p>            # \u5904\u7406\u8868\u5355\u6570\u636e<\/p>\n<p>            title = form.cleaned_data[&#39;title&#39;]<\/p>\n<p>            content = form.cleaned_data[&#39;content&#39;]<\/p>\n<p>            # \u4fdd\u5b58\u5230\u6570\u636e\u5e93<\/p>\n<p>            Article.objects.create(title=title, content=content)<\/p>\n<p>            return HttpResponse(&#39;Form submitted successfully.&#39;)<\/p>\n<p>    else:<\/p>\n<p>        form = ArticleForm()<\/p>\n<p>    return render(request, &#39;my_form.html&#39;, {&#39;form&#39;: form})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Flask<\/h4>\n<\/p>\n<p><p>Flask\u901a\u5e38\u4f7f\u7528WTForms\u4f5c\u4e3a\u8868\u5355\u5904\u7406\u548c\u9a8c\u8bc1\u5de5\u5177\u3002\u4f60\u9700\u8981\u5b89\u88c5Flask-WTF\u6269\u5c55\u6765\u4f7f\u7528WTForms\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Flask-WTF<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flask-wtf<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u5e94\u7528<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template, request<\/p>\n<p>from flask_wtf import FlaskForm<\/p>\n<p>from wtforms import StringField, TextAreaField<\/p>\n<p>from wtforms.validators import DataRequired<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>app.secret_key = &#39;super_secret_key&#39;<\/p>\n<p>class ArticleForm(FlaskForm):<\/p>\n<p>    title = StringField(&#39;Title&#39;, validators=[DataRequired()])<\/p>\n<p>    content = TextAreaField(&#39;Content&#39;, validators=[DataRequired()])<\/p>\n<h2><strong>\u5728\u89c6\u56fe\u4e2d\u5904\u7406\u8868\u5355<\/strong><\/h2>\n<p>@app.route(&#39;\/submit&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<\/p>\n<p>def submit():<\/p>\n<p>    form = ArticleForm()<\/p>\n<p>    if form.validate_on_submit():<\/p>\n<p>        title = form.title.data<\/p>\n<p>        content = form.content.data<\/p>\n<p>        # \u4fdd\u5b58\u5230\u6570\u636e\u5e93<\/p>\n<p>        new_article = Article(title=title, content=content)<\/p>\n<p>        db.session.add(new_article)<\/p>\n<p>        db.session.commit()<\/p>\n<p>        return &#39;Form submitted successfully.&#39;<\/p>\n<p>    return render_template(&#39;submit.html&#39;, form=form)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Flask\u4e2d\u5904\u7406\u548c\u9a8c\u8bc1\u8868\u5355\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u662fWeb\u5e94\u7528\u7a0b\u5e8f\u7684\u5173\u952e\u529f\u80fd\u3002Python Web\u6846\u67b6\u901a\u5e38\u63d0\u4f9b\u5185\u7f6e\u7684\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><h4>1. Django<\/h4>\n<\/p>\n<p><p>Django\u5185\u7f6e\u4e86\u4e00\u4e2a\u5f3a\u5927\u7684\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u6846\u67b6\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528Django\u7684\u8ba4\u8bc1\u7cfb\u7edf\u6765\u5904\u7406\u7528\u6237\u767b\u5f55\u3001\u6ce8\u518c\u548c\u6743\u9650\u7ba1\u7406\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from django.contrib.auth.forms import UserCreationForm<\/p>\n<p>from django.contrib.auth import login, authenticate<\/p>\n<p>from django.shortcuts import render, redirect<\/p>\n<h2><strong>\u7528\u6237\u6ce8\u518c\u89c6\u56fe<\/strong><\/h2>\n<p>def register(request):<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        form = UserCreationForm(request.POST)<\/p>\n<p>        if form.is_valid():<\/p>\n<p>            form.save()<\/p>\n<p>            username = form.cleaned_data.get(&#39;username&#39;)<\/p>\n<p>            password = form.cleaned_data.get(&#39;password1&#39;)<\/p>\n<p>            user = authenticate(username=username, password=password)<\/p>\n<p>            login(request, user)<\/p>\n<p>            return redirect(&#39;index&#39;)<\/p>\n<p>    else:<\/p>\n<p>        form = UserCreationForm()<\/p>\n<p>    return render(request, &#39;register.html&#39;, {&#39;form&#39;: form})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. Flask<\/h4>\n<\/p>\n<p><p>Flask\u901a\u5e38\u4f7f\u7528Flask-Login\u6269\u5c55\u6765\u5904\u7406\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u3002\u4f60\u9700\u8981\u5b89\u88c5Flask-Login\u6269\u5c55\u6765\u4f7f\u7528\u7528\u6237\u8ba4\u8bc1\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5Flask-Login<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install flask-login<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u914d\u7f6e\u5e94\u7528<\/strong>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from flask import Flask, render_template, redirect, url_for, request<\/p>\n<p>from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user<\/p>\n<p>app = Flask(__name__)<\/p>\n<p>app.secret_key = &#39;super_secret_key&#39;<\/p>\n<p>login_manager = LoginManager(app)<\/p>\n<h2><strong>\u7528\u6237\u6a21\u578b<\/strong><\/h2>\n<p>class User(UserMixin):<\/p>\n<p>    def __init__(self, id, username, password):<\/p>\n<p>        self.id = id<\/p>\n<p>        self.username = username<\/p>\n<p>        self.password = password<\/p>\n<h2><strong>\u7528\u6237\u6570\u636e<\/strong><\/h2>\n<p>users = {&#39;john_doe&#39;: User(id=1, username=&#39;john_doe&#39;, password=&#39;password&#39;)}<\/p>\n<p>@login_manager.user_loader<\/p>\n<p>def load_user(user_id):<\/p>\n<p>    for user in users.values():<\/p>\n<p>        if user.id == int(user_id):<\/p>\n<p>            return user<\/p>\n<p>    return None<\/p>\n<h2><strong>\u7528\u6237\u767b\u5f55\u89c6\u56fe<\/strong><\/h2>\n<p>@app.route(&#39;\/login&#39;, methods=[&#39;GET&#39;, &#39;POST&#39;])<\/p>\n<p>def login():<\/p>\n<p>    if request.method == &#39;POST&#39;:<\/p>\n<p>        username = request.form[&#39;username&#39;]<\/p>\n<p>        password = request.form[&#39;password&#39;]<\/p>\n<p>        user = users.get(username)<\/p>\n<p>        if user and user.password == password:<\/p>\n<p>            login_user(user)<\/p>\n<p>            return redirect(url_for(&#39;index&#39;))<\/p>\n<p>    return render_template(&#39;login.html&#39;)<\/p>\n<h2><strong>\u7528\u6237\u6ce8\u9500\u89c6\u56fe<\/strong><\/h2>\n<p>@app.route(&#39;\/logout&#39;)<\/p>\n<p>@login_required<\/p>\n<p>def logout():<\/p>\n<p>    logout_user()<\/p>\n<p>    return redirect(url_for(&#39;login&#39;))<\/p>\n<h2><strong>\u53d7\u4fdd\u62a4\u7684\u89c6\u56fe<\/strong><\/h2>\n<p>@app.route(&#39;\/index&#39;)<\/p>\n<p>@login_required<\/p>\n<p>def index():<\/p>\n<p>    return f&#39;Hello, {current_user.username}!&#39;<\/p>\n<p>if __name__ == &#39;__main__&#39;:<\/p>\n<p>    app.run(debug=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u4f60\u53ef\u4ee5\u5728Flask\u4e2d\u5b9e\u73b0\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u529f\u80fd\u3002<\/p>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u4f5c\u4e3a\u4e00\u79cd\u5f3a\u5927\u7684\u811a\u672c\u8bed\u8a00\uff0c\u5728Web\u5f00\u53d1\u4e2d\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u901a\u8fc7\u4f7f\u7528Web\u6846\u67b6\uff08\u5982Django\u548cFlask\uff09\u3001\u5904\u7406HTTP\u8bf7\u6c42\u3001\u7ba1\u7406\u4f1a\u8bdd\u3001\u6a21\u677f\u6e32\u67d3\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u3001\u8868\u5355\u5904\u7406\u548c\u9a8c\u8bc1\u3001\u7528\u6237\u8ba4\u8bc1\u548c\u6388\u6743\u7b49\u65b9\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5feb\u901f\u6784\u5efa\u9ad8\u6548\u4e14\u529f\u80fd\u4e30\u5bcc\u7684Web\u5e94\u7528\u7a0b\u5e8f\u3002\u65e0\u8bba\u662f\u9009\u62e9Django\u8fd8\u662fFlask\uff0cPython\u90fd\u80fd\u591f\u63d0\u4f9b\u7075\u6d3b\u4e14\u5f3a\u5927\u7684\u5de5\u5177\u6765\u6ee1\u8db3Web\u5f00\u53d1\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5f00\u53d1\u7f51\u7ad9\uff1f<\/strong><br \/>Python\u662f\u4e00\u79cd\u529f\u80fd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u7f51\u7ad9\u5f00\u53d1\u3002\u9996\u5148\uff0c\u53ef\u4ee5\u9009\u62e9\u5408\u9002\u7684Web\u6846\u67b6\uff0c\u5982Django\u6216Flask\uff0c\u8fd9\u4e9b\u6846\u67b6\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u5e2e\u52a9\u5f00\u53d1\u8005\u5feb\u901f\u6784\u5efa\u7f51\u7ad9\u3002\u6b64\u5916\uff0c\u7ed3\u5408HTML\u3001CSS\u548cJavaScript\uff0c\u53ef\u4ee5\u521b\u5efa\u52a8\u6001\u548c\u54cd\u5e94\u5f0f\u7684\u7f51\u7ad9\u754c\u9762\u3002\u901a\u8fc7\u5b66\u4e60\u5982\u4f55\u4f7f\u7528\u6570\u636e\u5e93\uff08\u5982SQLite\u6216PostgreSQL\uff09\uff0c\u60a8\u53ef\u4ee5\u5b58\u50a8\u548c\u7ba1\u7406\u7528\u6237\u6570\u636e\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u7f51\u7ad9\u7684\u529f\u80fd\u3002<\/p>\n<p><strong>Python\u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d\u7684\u4f18\u52bf\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>Python\u56e0\u5176\u7b80\u6d01\u7684\u8bed\u6cd5\u548c\u5f3a\u5927\u7684\u5e93\u800c\u53d7\u5230\u5f00\u53d1\u8005\u7684\u9752\u7750\u3002\u5728\u7f51\u7ad9\u5f00\u53d1\u4e2d\uff0cPython\u80fd\u591f\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\uff0c\u51cf\u5c11\u4ee3\u7801\u91cf\uff0c\u6613\u4e8e\u7ef4\u62a4\u3002\u6b64\u5916\uff0cDjango\u548cFlask\u7b49\u6846\u67b6\u5185\u7f6e\u4e86\u8bb8\u591a\u5b89\u5168\u7279\u6027\uff0c\u5982\u9632\u6b62SQL\u6ce8\u5165\u548c\u8de8\u7ad9\u811a\u672c\u653b\u51fb\uff0c\u786e\u4fdd\u7f51\u7ad9\u7684\u5b89\u5168\u6027\u3002\u540c\u65f6\uff0cPython\u7684\u793e\u533a\u652f\u6301\u6d3b\u8dc3\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u8f7b\u677e\u83b7\u53d6\u8d44\u6e90\u548c\u89e3\u51b3\u65b9\u6848\uff0c\u52a0\u901f\u5f00\u53d1\u8fc7\u7a0b\u3002<\/p>\n<p><strong>\u6211\u9700\u8981\u54ea\u4e9b\u5de5\u5177\u548c\u8d44\u6e90\u6765\u5f00\u59cbPython\u7f51\u7ad9\u5f00\u53d1\uff1f<\/strong><br \/>\u8981\u5f00\u59cbPython\u7f51\u7ad9\u5f00\u53d1\uff0c\u60a8\u9700\u8981\u5b89\u88c5Python\u89e3\u91ca\u5668\u548c\u76f8\u5e94\u7684\u5f00\u53d1\u73af\u5883\uff0c\u63a8\u8350\u4f7f\u7528Visual Studio Code\u6216PyCharm\u4f5c\u4e3aIDE\u3002\u6b64\u5916\uff0c\u4e86\u89e3HTML\u3001CSS\u548cJavaScript\u662f\u5fc5\u4e0d\u53ef\u5c11\u7684\uff0c\u56e0\u4e3a\u5b83\u4eec\u662f\u6784\u5efa\u7f51\u9875\u7684\u57fa\u7840\u3002\u53ef\u4ee5\u8bbf\u95ee\u5b98\u65b9\u6587\u6863\u3001\u5728\u7ebf\u6559\u7a0b\u548c\u793e\u533a\u8bba\u575b\uff0c\u83b7\u53d6\u5b66\u4e60\u8d44\u6e90\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u5e2e\u52a9\u60a8\u5728Python\u7f51\u7ad9\u5f00\u53d1\u4e2d\u9010\u6b65\u63d0\u5347\u6280\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u4f5c\u4e3a\u7f51\u7ad9\u811a\u672c\u8bed\u8a00\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528Web\u6846\u67b6\u3001\u5904\u7406HTTP\u8bf7\u6c42\u3001\u7ba1\u7406\u4f1a\u8bdd\u3001\u6a21\u677f\u6e32\u67d3\u3001\u6570\u636e\u5e93\u64cd\u4f5c\u7b49 [&hellip;]","protected":false},"author":3,"featured_media":1046537,"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\/1046518"}],"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=1046518"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1046518\/revisions"}],"predecessor-version":[{"id":1046541,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1046518\/revisions\/1046541"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1046537"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1046518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1046518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1046518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}